在python中创建操作菜单

在python中创建操作菜单,python,shell,python-2.7,python-3.x,while-loop,Python,Shell,Python 2.7,Python 3.x,While Loop,1) 到目前为止,我在while循环中有此代码,但我只想循环12次: print ("Please enter the 12 monthly figures") input ("Enter a value in the range 0 to 300:") 我试过一个for循环,但没有成功 2) 我想为我的代码创建一个菜单,到目前为止,我有: print ("Please choose one of the following options:") ans=True while ans:

1) 到目前为止,我在while循环中有此代码,但我只想循环12次:

print ("Please enter the 12 monthly figures")
input ("Enter a value in the range 0 to 300:")
我试过一个for循环,但没有成功

2) 我想为我的代码创建一个菜单,到目前为止,我有:

print ("Please choose one of the following options:")

ans=True
while ans:
    print ("""
    0. Quit
    1. Work out and display the total
    3. Work out and display the mean 
    4. Work out and display the standard deviation
    5. Work out and display the median 
    6. Work out and display the lowest and second lowest
    7. Work out and display the 3 month 
    8. Work out and display the months 
    9. Work out display  level
    """)

但是我想让用户选择一个

Python显然没有switch/case循环,所以您可以做的一件事就是构建一些if语句。如果您使用的是2.7,您将使用原始输入进行用户输入,如果是3.x,您将只使用输入

if input == 0:
    print ("You picked zero\n")
    ...
等等。此外,我认为如果你把int(输入)或任何你分配给你的输入,它会工作,因为输入需要一个字符串,所以你必须转换它

1)您可以将
range()
与for循环一起使用,例如:

for i in range(0, 12):
    print(i)
if a == 0:
    print("something")

elif a == 1:
    print("something else")

elif a == 2:
    print("another something")
2) 您可以对多个可能的值使用一系列
if
elif
语句,例如:

for i in range(0, 12):
    print(i)
if a == 0:
    print("something")

elif a == 1:
    print("something else")

elif a == 2:
    print("another something")
它们所做的是,首先检查第一条语句是否为真,如果不是真的,则转到下一条语句,直到没有剩余语句或其中一条语句为真。 希望这有帮助。

试试这个:

def get_monthly_rainfall_figures():
    rainfall_figures = []
    print("Please enter the 12 monthly rainfall figures")
    for month in range(12):
        in_ = int(input("Enter a value (0-300): "))
        if 0 <= in_ <= 300:
            rainfall_figures.append(in_)
        else:
            # handle invalid input
    return rainfall_figures

你现在的密码是什么?