Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 文本菜单没有';不能正确打印_Python_Menu - Fatal编程技术网

Python 文本菜单没有';不能正确打印

Python 文本菜单没有';不能正确打印,python,menu,Python,Menu,问题已经解决了。然而,我的帐户是新的,所以它不会让我回答这个问题。似乎用raw_input()更改input()的实例可以使其正常工作。老实说,我不知道为什么,但这可能与2和3之间的差异有关 我要做一个可以计算面积和周长的程序。那部分并不难 但是,菜单不起作用。菜单的第一部分工作得很好,但在您选择后,它不会打印它应该打印的内容 import math print (""" 1) Calculate circumference 2) Calculate area """) ans = input

问题已经解决了。然而,我的帐户是新的,所以它不会让我回答这个问题。似乎用raw_input()更改input()的实例可以使其正常工作。老实说,我不知道为什么,但这可能与2和3之间的差异有关

我要做一个可以计算面积和周长的程序。那部分并不难

但是,菜单不起作用。菜单的第一部分工作得很好,但在您选择后,它不会打印它应该打印的内容

import math

print ("""
1) Calculate circumference
2) Calculate area
""")
ans = input("Enter 1, 2, or 0 to exit this program: ")
if ans == "1":
    diameter = float(input("Enter the diameter: "))
    if diameter > 0:
        circumference = (diameter*math.pi)
        print("The circumference is", circumference)
    else:
        print("Error: the diameter must be a positive number.")
if ans == "2":
    radius = float(input("Enter the radius: "))
    if radius > 0:
        area = ((radius**2)*math.pi)
        print("The area is", area)
    else:
        print("Error: the radius must be a postive number.")
if ans == "0":
    print("Thanks for hanging out with me!")
    quit()

代码的缩进显示每个“if”、“for”或“while”的控制范围。 您需要进一步缩进每个主要“if”语句下的说明,以便 它们仅在选择“if”语句时执行。否则,一切 也就是说,每次通过循环都会执行最左边的缩进。例如,您应该有如下内容:

if answer == '1':
....<statements that execute under condition 1>
elif answer == '2':
....<statements that execute under condition 2>
如果答案='1':
....
elif答案==“2”:
....

如果我用“…..”强调缩进,正确缩进后,将
如果ans==“1”
更改为
str(ans)=“1”
ans==1
,应该可以

这应该起作用:

import math

print ("""
1) Calculate circumference
2) Calculate area
""")
ans = input("Enter 1, 2, or 0 to exit this program: ")
if str(ans) == "1":
    diameter = input("Enter the diameter: ")
    print diameter
    if float(diameter) > 0.0:
        circumference = (diameter*math.pi)
        print("The circumference is", circumference)
    else:
        print("Error: the diameter must be a positive number.")
....

PS:正如在评论中提到的,它是有效的,但它是令人厌恶的。如果使用python的话,我们应该只使用
ans==1
或者将
input修改为input\u raw
,第二个if语句不在第一个if语句的括号内。1和2都是如此

if ans == "1";
    diameter = float(input("enter the diameter: "))
    if diameter ....

对我来说,把ans当作整数是有效的。我的意思是把ans当作整数,而不是写ans==“1”,我写的是ans==1,它打印正确的值。检查此代码:



它打印什么?您希望它打印什么?请对照此检查原始代码的缩进。你发布的内容会产生一个
缩进错误
。这是你真正的缩进吗?尝试代替?因此,你现在的任务是添加答案,并说明解决问题的原因。他在你发布此内容前一分钟左右的最后一次编辑中解决了这个问题。这也很有效。谢谢你,伙计
import math

print ("""
1) Calculate circumference
2) Calculate area
""")
ans = input("Enter 1, 2, or 0 to exit this program: ")
print ans
if ans ==1:
    diameter = float(input("Enter the diameter: "))
    if diameter > 0:
        circumference = (diameter*math.pi)
        print("The circumference is", circumference)
    else:
        print("Error: the diameter must be a positive number.")
if ans == 2:
    radius = float(input("Enter the radius: "))
    if radius > 0:
        area = ((radius**2)*math.pi)
        print("The area is", area)
    else:
        print("Error: the radius must be a postive number.")
if ans == 0:
    print("Thanks for hanging out with me!")
    quit()