2017-04-06 50 views
1

我无法调用这些功能。所有已创建的功能都正常工作。我只是无法从菜单调用函数。我对python仍然很陌生,所以如果你看到一些没有意义的东西,请告诉我。无法从菜单调用功能

#function for lottery generator 
def lottery_number_generator(): 
    import random 
    lottoNumber = [] 

    for i in range(0,7): 
     randNum = random.randint(0,9) 
     lottoNumber.append(randNum) 

    for i in range(0,7): 
     print lottoNumber[i], 

#function for number analysis 
def number_analysis(): 
    total = 0.0 
    average = 0.0 
    numberInput = 0.0 
    numbers = [] 

    print("Enter in a series of 20 numbers\n") 

    for i in range(20): 
     numberInput = input("Enter your %s number: " % (i+1)) 
     numbers.append(numberInput) 
     total += numberInput 
    average = total/20 

    print ("\nThe highest number is %.2f: " % max(numbers)) 
    print ("The lowest number is %.2f: " % min(numbers)) 
    print ("Your total is %.2f: " % total) 
    print ("Your average is %.2f: " %average) 

#function for rainfall calculator 
def rainfall(): 
    totRain = 0.0 
    average = 0.0 
    totalRain = 0.0 
    months = ["January", "Feburary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] 
    rainFall = [] 
    for i in range(0,12): 
     monthRain = input("Enter rainfall inches for %s: " % months[i]) 
     rainFall.append(monthRain) 
     totalRain += monthRain 

    average = float(totalRain/12) 

    for i in range(0,12): 
     print '' 
     print("Rainfall for %s is %i" %(months[i],rainFall[i])) 

    print ("\nThe total rainfall was %.2f: " % totalRain) 
    print ("The average rainfall is %.2f" % average) 
    print ("The highest amount of rainfall was %.2f "% max(rainFall)) 
    print ("The lowest amount of rainfall was %.2f "% min(rainFall)) 

#function for total sales 
def total_sales(): 
    day = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"] 
    dailySales = [] 
    sales = 0.0 
    totalSales = 0.0 

    for i in range(0,7): 
     sales = input("Enter sales for %s " % day[i]) 
     dailySales.append(sales) 
     totalSales += sales  

    for i in range(0,7): 
     print ("Sales for %s is %.2f:" % (day[i],dailySales[i])) 
    print ("For a total of %.2f: " % totalSales) 
def menu(): 
    print ("1. Play lottery number generator.\n2. Play number analysis.\n3. Play rainfall calculator.\n4. Play total sales calculator.\n5. Exit/Quit.") 
    input() 
def main(): 
    while True: 
     if menu() == 1: 
      lottery_number_generator() 
     elif menu() == 2: 
      number_analysis() 
     elif menu() == 3: 
      rainfall() 
     elif menu() == 4: 
      total_sales() 
     elif menu() == 0: 
      quit 
     else: 
      print ("\n Not a valid choice try AGAIN.") 

#start of program 


print ("Welcome, Please select which HW assignment you would like to view:\nPlease select which HW assignment to play by pushing the number.\n") 

menu() 
main() 
+0

我想你想在你的菜单函数中返回input()。你也不需要在你的代码的第二行调用'menu()',只需'main()'就足够了。 – umutto

回答

1

正如umutto建议,你需要从input()返回的选择:

def menu(): 
    print ("1. Play lottery number generator.\n2. Play number analysis.\n3. Play rainfall calculator.\n4. Play total sales calculator.\n5. Exit/Quit.") 
    return input() 

,那么你可能想选择每次迭代只有一次,然后测试它的值:

def main(): 
    while True: 
     choice = menu() 
     if choice == 1: 
      lottery_number_generator() 
     elif choice == 2: 
      number_analysis() 
     elif choice == 3: 
      rainfall() 
     elif choice == 4: 
      total_sales() 
     elif choice == 0: 
      quit 
     else: 
      print ("\n Not a valid choice try AGAIN.") 

#start of program 


print ("Welcome, Please select which HW assignment you would like to view:\nPlease select which HW assignment to play by pushing the number.\n") 

main() 

注意:根据您的python版本,您可能需要小心处理字符串"1"或数字1当您测试值choice ...

+0

非常感谢您的帮助,我现在可以开展工作。 –