Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.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 While/if循环给我带来麻烦(初学者)_Python_If Statement_For Loop_While Loop - Fatal编程技术网

Python While/if循环给我带来麻烦(初学者)

Python While/if循环给我带来麻烦(初学者),python,if-statement,for-loop,while-loop,Python,If Statement,For Loop,While Loop,我一直在尝试制作我的第一个“solo”python程序,它是一个计算器,您可以选择希望它计算什么样的公式,然后输入所需的变量。我在while/for循环中遇到了问题,当我运行程序时,我得到了正确的菜单:menu(),然后当我通过输入1选择下一个菜单时,我正确地得到了“v_菜单”,但是如果我输入2,应该得到“m_菜单”,我只会得到v_菜单,就像我输入1一样 我希望我的解释有道理,我对这一切还是很陌生的。谢谢你给我的任何帮助,我为此伤了脑筋至少有一个小时了 干杯,这是我的密码: #编码=utf-8

我一直在尝试制作我的第一个“solo”python程序,它是一个计算器,您可以选择希望它计算什么样的公式,然后输入所需的变量。我在while/for循环中遇到了问题,当我运行程序时,我得到了正确的菜单:menu(),然后当我通过输入1选择下一个菜单时,我正确地得到了“v_菜单”,但是如果我输入2,应该得到“m_菜单”,我只会得到v_菜单,就像我输入1一样

我希望我的解释有道理,我对这一切还是很陌生的。谢谢你给我的任何帮助,我为此伤了脑筋至少有一个小时了

干杯,这是我的密码: #编码=utf-8

#Menues
def menu():
    print "Choose which topic you want in the list below by typing the corresponding number\n"
    print "\t1) virksomhedsøkonomi\n \t2) matematik\n"
    return raw_input("type the topic you want to pick\n >>")


def v_menu():
    print "Choose which topic you want in the list below by typing the corresponding number"
    print "\t1) afkastningsgrad\n \t2) overskudsgrad\n \t3) aktivernes omsætningshastighed\n \t4)                          Egenkapitalens forrentning\n \t5) return to main menu\n"
    return raw_input("Type the topic you want to pick\n >>")

def m_menu():
    print "Choose which topic you want in the list below by typing the corresponding number"
    print "\t1) omregn Celsius til Fahrenheit\n \t2) omregn Fahrenheit til Celsius\n"
    return raw_input("Type the topic you want to pick\n >>")

    # - Mat -

#Celsius to Fahrenheit
def c_to_f():
    c_temp = float(raw_input("Enter a temperatur in Celsius"))
    #Calculates what the temperatur is in Fahrenheit
    f_temp = c_temp * 9 / 5 + 32
    #Prints the temperatur in Fahrenheit
    print (str(c_temp) + " Celsius is equal to " + str(f_temp) + " Fahrenheit")


#Fahrenheit to Celsius
def f_to_c(): 
    f_temp = float(raw_input("Enter a temperatur in Fahrenheit"))
    #Calculates what the temperatur is in celsius
    c_temp = (f_temp - 32) * (float(100) / 180)
    #Prints the temperatur in celsius
    print (str(f_temp) + " Fahrenheit is equal to " + str(c_temp) + " Celsius")


#Program
loop = 1
choice = 0



while loop == 1:
    choice = menu()

    if choice == "1" or "1)":
        v_menu()

    elif choice == "2" or "2)":
        m_menu()
        if choice == "1":
            c_to_f()
        elif choice == "2":
            f_to_c()

    loop = 0

您的问题在于if语句:if choice==“1”或“1”:

您真正需要的是:如果choice==“1”或choice==“1”):

or之后的所有内容都将作为另一个表达式进行计算。你说的是“如果选择等于一,或者存在一个。”

在本例中,“1”的计算结果为“true”,因此您将始终点击该分支。

问题就在这里

if choice == "1" or "1)":
    v_menu()

elif choice == "2" or "2)":
你必须像这样写它们

if choice == "1" or choice == "1)":
        v_menu()

elif choice == "2" or choice == "2)":

否则,if语句始终为
True
。如果第一个
if
语句是
True
,则您的
elif
语句将不起作用。这就是为什么你不能调用
v_menu()

我刚刚编辑了一些错误,应该在我的代码段的底部?从…开始。While loop==1:感谢您的快速回答,一旦我在这方面做得更好,我会尽最大努力回答其他人的问题;)
如果在{“1”,“1”中选择}
^^ty,将其改为该选项,将节省大量空间,因为我想添加一些更有效的输入。无需担心,设置查找是
0(1)
,因此在较大的数据集中,设置查找是一个很好的方法。啊!谢谢你!该死的,我甚至记得读过这条规则,真傻,一旦有人告诉你,这个问题总是那么明显。Python中的首选方法是
如果在{“1”,“1”中选择:
。说得好,瑞克!OP:这里的paren只是表示它是一个元组。没有什么特别的,同样的语法也适用于列表或字典!在看到您关于它是一个元组的评论之前,我实际上将它编辑成了一个
集。有人告诉我,在
if
语句中,使用
集合
更为优越。谢谢!非常感谢