如何获取或初始化我想要的变量?

我正在尝试编写一个简单的程序,但我遇到了一个问题,即该程序最终没有输出给定的变量“tax”。


def main():

    # define and initialize constants and variables

    menu1 = 6

    menu2 = 2.5

    menu3 = 1.25

    menu4 = 3.75

    choose = total = 0

    tax = total*0.06

    

    # display welcome

    print("Welcome to Yum Yum Snack Bar!")

    try:

        while choose != 5:

            print("\nPlease choose from the following menu:")

            print("1) Personal Pizza $6.00")

            print("2) Pretzel $2.50")

            print("3) Chips $1.25")

            print("4) Hot Dog $3.75")

            print("5) Exit ")

            choose = int(input("\nEnter your choice here: "))

            if choose == 1:

                total += menu1

            elif choose == 2:

                total += menu2

            elif choose == 3:

                total += menu3

            elif choose == 4:

                total += menu4

            elif choose == 5:

                continue

            else:

                print("Invalid choice. Must choose 1 – 5. Try again.")

            print("Current total: $",total)

    except:

        print("Invalid choice. Must choose 1 – 5. Try again.")

        main()

    print("Current total: $",total)

    print("Sales tax: $",tax)

    print("Total Bill: $",total+tax)

    print("Have a nice day!")

main()


HUH函数
浏览 108回答 3
3回答

守着星空守着你

当你初始化时tax,你给了它一个值,0因为total*0.06在那一点上等于零。python 逐行进行,因此变量“ tax”在整个代码中没有更改其值,您只更改了“ total”。所以要得到税,你应该重新计算。print("Current total: $",total)tax=0.06*totalprint("Sales tax: $",tax)print("Total Bill: $",total+tax)print("Have a nice day!")

慕少森

在这里,总计的值得到更新,但税金是在检查之前计算的,如下所示,因此税金将tax = total*0.06在最初的地方输出total=0 请试试这个:def main():    # define and initialize constants and variables    menu1 = 6    menu2 = 2.5    menu3 = 1.25    menu4 = 3.75    choose = total = tax = 0    # display welcome    print("Welcome to Yum Yum Snack Bar!")    try:        while choose != 5:            print("\nPlease choose from the following menu:")            print("1) Personal Pizza $6.00")            print("2) Pretzel $2.50")            print("3) Chips $1.25")            print("4) Hot Dog $3.75")            print("5) Exit ")            choose = int(input("\nEnter your choice here: "))            if choose == 1:                total += menu1            elif choose == 2:                total += menu2            elif choose == 3:                total += menu3            elif choose == 4:                total += menu4            elif choose == 5:                continue            else:                print("Invalid choice. Must choose 1 – 5. Try again.")            print("Current total: $",total)    except:        print("Invalid choice. Must choose 1 – 5. Try again.")    tax = total*0.06    print("Current total: $",total)    print("Sales tax: $",tax)    print("Total Bill: $",total+tax)    print("Have a nice day!")main()

哔哔one

我想你的问题已经解决了。也开始使用 python 字典而不是使用大量的 if else 语句def main():&nbsp; &nbsp; # define and initialize constants and variables&nbsp; &nbsp; menu1 = 6&nbsp; &nbsp; menu2 = 2.5&nbsp; &nbsp; menu3 = 1.25&nbsp; &nbsp; menu4 = 3.75&nbsp; &nbsp; choose = total = 0&nbsp; &nbsp; tax = total*0.06&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; # display welcome&nbsp; &nbsp; print("Welcome to Yum Yum Snack Bar!")&nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; while choose != 5:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("\nPlease choose from the following menu:")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("1) Personal Pizza $6.00")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("2) Pretzel $2.50")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("3) Chips $1.25")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("4) Hot Dog $3.75")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("5) Exit ")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; choose = int(input("\nEnter your choice here: "))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; price_dict = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1: menu1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2: menu2,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3: menu3,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4: menu4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if 0 < choose < 5:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; total += price_dict[choose]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if choose == 5:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Invalid choice. Must choose 1 – 5. Try again.")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Current total: $",total)&nbsp; &nbsp; except:&nbsp; &nbsp; &nbsp; &nbsp; print("Invalid choice. Must choose 1 – 5. Try again.")&nbsp; &nbsp; &nbsp; &nbsp; main()&nbsp; &nbsp; tax = total*0.06&nbsp; &nbsp; print("Current total: $",total)&nbsp; &nbsp; print("Sales tax: $",tax)&nbsp; &nbsp; print("Total Bill: $",total+tax)&nbsp; &nbsp; print("Have a nice day!")main()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python