如何在多个if / elif语句中使用while循环?

时间:2019-02-09 16:55:11

标签: python while-loop

尝试使用while语句返回到打开提示。 我为我的垃圾代码道歉。

我已经尝试为while循环分配一个True / False值,但是该程序只是在给出任何输入后才终止。

choice = eval(input('What would you like to convert? \n Farenheit to Celcius (1) \n Feet to Meters (2) \n Pounds to Kilograms (3) \n Ounces to Liters (4) \n : '))

 while choice:

    if choice == 1:

        degreesF = eval(input('Enter the temperature in degrees F: '))
        degreesC = 5/9*(degreesF - 32)
        print(degreesC, 'degrees Celcius')

    elif choice == 2: 

        distanceFeet = eval(input('Enter the distance in feet: '))
        distanceMeters = distanceFeet/3.28
        print(distanceMeters, 'm')

    elif choice == 3:

        Pounds = eval(input('Pounds: '))
        Kilograms = Pounds*0.45359237038
        print(Kilograms, 'kg')

    elif choice == 4:

        Ounces = eval(input('Ounces: '))
        Liters = Ounces*0.0295735
        print(Liters, 'L')

当前,程序使我返回到将输入设置为的状态。例如,如果输入1,则可以转换温度,但只能转换温度。

4 个答案:

答案 0 :(得分:3)

while True内的行下放置可能会产生预期的结果:

choice = eval(input('What would you like to convert? \n Farenheit to Celcius (1) \n Feet to Meters (2) \n Pounds to Kilograms (3) \n Ounces to Liters (4) \n : '))

如果我没记错,则需要以下内容:

while True:
    choice = eval(input('What would you like to convert? \n Farenheit to Celcius (1) \n Feet to Meters (2) \n Pounds to Kilograms (3) \n Ounces to Liters (4) \n : '))

    if choice == 1:

        degreesF = eval(input('Enter the temperature in degrees F: '))
        degreesC = 5/9*(degreesF - 32)
        print(degreesC, 'degrees Celcius')

    elif choice == 2: 

        distanceFeet = eval(input('Enter the distance in feet: '))
        distanceMeters = distanceFeet/3.28
        print(distanceMeters, 'm')

    elif choice == 3:

        Pounds = eval(input('Pounds: '))
        Kilograms = Pounds*0.45359237038
        print(Kilograms, 'kg')

    elif choice == 4:

        Ounces = eval(input('Ounces: '))
        Liters = Ounces*0.0295735
        print(Liters, 'L')

答案 1 :(得分:3)

您希望将输入内容包含在while循环中:

while True:
    choice = int(input('What would you like to convert? \n Farenheit to Celcius (1) \n Feet to Meters (2) \n Pounds to Kilograms (3) \n Ounces to Liters (4) \n : '))

    if choice == 1:
        degreesF = float(input('Enter the temperature in degrees F: '))
        degreesC = 5/9*(degreesF - 32)
        print(degreesC, 'degrees Celcius')
    elif choice == 2: 
        distanceFeet = float(input('Enter the distance in feet: '))
        distanceMeters = distanceFeet/3.28
        print(distanceMeters, 'm')
    elif choice == 3:
        Pounds = float(input('Pounds: '))
        Kilograms = Pounds*0.45359237038
        print(Kilograms, 'kg')
    elif choice == 4:
        Ounces = float(input('Ounces: '))
        Liters = Ounces*0.0295735
        print(Liters, 'L')
    else:
        break

答案 2 :(得分:1)

您必须将eval行放在while循环内,才能多次运行。

未经测试的(伪)代码:

while true:
your input line
your other code

这将永远运行,所以我建议做这样的事

while true:
  your input line
  if input == 0:
    break
  your other code

这将在您键入0时停止while循环

答案 3 :(得分:1)

您需要做几件事,现在您应该使它们明确:

  1. 您的程序应该永远运行,或者直到被告知退出。

  2. 您选择菜单的提示应循环播放,直到获得有效答案为止。

  3. 您输入的数字值应循环显示,直到获得有效答案为止。

解决方案的一般形式

您怎么做这些事情?通常,从错误的初始数据开始,然后循环直到看到好的数据:

some_value = bad_data()
while some_value is bad:
    some_value = input("Enter some value: ")

bad_data()的良好“默认”值为特殊值None。您可以编写:

some_value = None

另一方面,您的is bad测试可能需要其他类型的错误数据,例如字符串。您可能考虑使用''作为错误数据值:

some_value = ''

最后,如果您的is bad测试想要一个整数值,则可以考虑使用一个您认为不好的数字或超出范围的数字(例如负值):

some_value = 100
# or 
some_value = -1

针对您的特定问题:

1。我如何永远运行?

您可以使用永不退出的while循环来永远运行。只要 condition 为true,就会运行while循环。在Python中是否存在一个始终为真的值?是!名称为True

while True:
    # runs forever

2。在菜单选择有效之前,如何循环播放?

使用上面的一般形式进行整数选择。您的菜单要求用户输入数字。您可以使用 str .isdigit()或使用try:/except:检查有效值。在python中,例外是更好的选择:

choice = -1                           # Known bad value
while choice not in {1, 2, 3, 4}:     # {a,b,c} is a set
    instr = input('Choose 1..4')
    try:
        choice = int(instr)
    except:
        choice = -1

3。在获得有效数值之前如何循环播放?

对于像温度这样的浮点数,您不想尝试拼出一组明确的允许答案或一系列数字。相反,请使用None(您可以使用Nan,但相同结果需要更多字符)。

temp = None
while temp is None:
    instr = input('Enter a temp: ')

    try:
        temp = float(instr)
    except:
        temp = None

请注意,“ nan”是有效的浮点数。因此,您可能需要检查并禁止它。

我如何结合所有这些东西?

您将它们拼在一起:

while True:   # run forever

    # Menu choice:
    choice = -1                           # Known bad value
    while choice not in {1, 2, 3, 4}:     # {a,b,c} is a set
        instr = input('Choose 1..4')
        try:
            choice = int(instr)
        except:
            choice = -1

    if choice == 1:
        # Now read temps
        temp = None
        while temp is None:
            instr = input('Enter a temp: ')

            try:
                temp = float(instr)
            except:
                temp = None