-2

* > 反复循环,提示用户选择一个选项,然后运行

适当的命令并重复,直到用户选择退出。选项有: 0 退出程序;1 计算特定值集合的流速;2 计算雷诺数,3 绘制预期流量。如果用户选择任何其他选项,程序必须不断要求他们选择一个有效值,直到他们这样做。*

而我<=3:

i=input (" Please enter a number between 0-3:")

if i==0:
    print " program will exit."
    break

elif i==1:
     print ( " To calculate flow rate please input these values:" )
     pressure = input (" Please enter pressure drop in pascals: " )
     radius = input  (" Please enter radius in m: " )
     viscosity = input  (" Please enter viscosity of liquid in Pa.s: ")
     Length= input  (" Please enter length in m: " )
     Flowrate = (pressure * pi *(radius**4))/ ( 8 * viscosity * Length)
     print " Therefore, flow rate is " , Flowrate
     print " Program will run again for user to calculate flow rate..."


elif i==2:
    print ( " To calculate reynolds number please input these values:" )
    q=input ("Please enter a value for flow rate in m^3/s")
    radius = input  (" Please enter radius in m: " )
    density=input   (" Please enter density in kg/m^3 : " )
    viscosity = input  (" Please enter viscosity of liquid in Pa.s: ")
    Rno=(2*q*radius*density)/(viscosity*pi*radius*radius)
elif i==3:
    print " not implemented yet."
else:
    print " Program will start again!"                          





    ***new edited post***
4

3 回答 3

4

我可以发现的一个错误是:

Rno=(2*q*radius*density)/(viscosity*pi*radius*radius

您的代码缺少右括号。

另一个(逻辑)错误是input()调用应该在循环内,而不是在循环外。

于 2013-03-31T07:02:50.343 回答
1

我通常会让人们布置这类程序,比如

while(True):
    i = input("<Prompt>")

    if(i==1):
        # Do something

    elif(i==2):
        # Do something else

    #... any additional elifs

    else: 
        break

或者像这样

while(True):
    i = input("<Prompt>")

    if(i==1):
        # Do something

    elif(i==2):
        # Do something else

    #... any additional elifs

    elif(i==10):        # Assuming 10 here is the "exit" option
        break

    else: 
        print("Unknown selection, please try again")

第二个版本要求用户明确选择“退出”选项,而第一个版本将在选择未知/未定义选项时简单地退出。

于 2013-03-31T07:06:07.747 回答
0

你说过你的程序应该在用户进入后退出0,所以我会改变这个:

if i==0 :
    print " program will exit."
    i=4  

对此:

if i==0 :
    print " program will exit."
    break  

而且,你不需要在第二个中赋值0,因为在这个块中已经等于,所以你可以安全地删除这行代码。iifi0

于 2013-03-31T07:05:34.197 回答