【问题标题】:Creating an operational menu in python在 python 中创建操作菜单
【发布时间】:2015-01-21 15:53:29
【问题描述】:

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 shell python-2.7 python-3.x while-loop


【解决方案1】:

Python 显然没有 switch/case 循环,因此您可以做的一件事是构建一些 if 语句。如果您使用的是 2.7,您将使用 raw_input 作为用户输入,如果是 3.x,您将只使用输入。

if input == 0:
    print ("You picked zero\n")
    ...

等等。另外,我认为如果你输入 int(input) 或任何你分配输入的东西,它会起作用,因为输入需要一个字符串,所以你必须转换它。

【讨论】:

    【解决方案2】:

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

    for i in range(0, 12):
        print(i)
    

    2) 您可以对多个可能的值使用一系列ifelif 语句,例如:

    if a == 0:
        print("something")
    
    elif a == 1:
        print("something else")
    
    elif a == 2:
        print("another something")
    

    它们的作用是,首先它检查第一个语句是否为 True,然后如果它不是 True,则转到下一个语句,直到没有任何语句或其中一个语句为 True。 希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      试试这个:

      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
      

      def menu():
          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
      """)
          user_in = input(">>")
          responses = {"0": quit_func,
                       "1": total_func,
                       "3": mean_func,
                      ...etc...}
          # where quit_func, total_func, etc are functions that do the described
          # action
          # This design pattern is known as a hash table, and is very idiomatic
          # in Python. In other languages you might use a switch/case block.
          try:
              responses[user_in]()
          except KeyError:
              # handle invalid input
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-05-15
        • 2014-06-25
        • 2013-11-26
        • 1970-01-01
        • 1970-01-01
        • 2017-06-18
        • 2012-12-21
        相关资源
        最近更新 更多