无法从菜单调用功能

时间:2017-04-06 01:25:40

标签: python

我无法调用这些函数。所有已创建的功能都可以正常工作。我只是在菜单中调用函数时遇到问题。我仍然是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()

1 个答案:

答案 0 :(得分: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