首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法从菜单调用函数

无法从菜单调用函数
EN

Stack Overflow用户
提问于 2017-04-06 01:25:39
回答 1查看 58关注 0票数 1

我打电话来是有困难的。所有已创建的函数都可以正常工作。我只是很难从菜单上调用函数。我对python还是很陌生的,所以如果你看到一些没有意义的东西,请告诉我。

代码语言:javascript
复制
#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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-06 01:52:31

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

代码语言:javascript
复制
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()

然后,您可能希望每次迭代只获得一次选择,然后测试其值:

代码语言:javascript
复制
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.

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43244170

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档