亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何獲取或初始化我想要的變量?

如何獲取或初始化我想要的變量?

HUH函數 2023-03-22 10:52:37
我正在嘗試編寫一個簡單的程序,但我遇到了一個問題,即該程序最終沒有輸出給定的變量“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()
查看完整描述

3 回答

?
守著星空守著你

TA貢獻1799條經驗 獲得超8個贊

當你初始化時tax,你給了它一個值,0因為total*0.06在那一點上等于零。


python 逐行進行,因此變量“ tax”在整個代碼中沒有更改其值,您只更改了“ total”。


所以要得到稅,你應該重新計算。


print("Current total: $",total)

tax=0.06*total

print("Sales tax: $",tax)

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

print("Have a nice day!")


查看完整回答
反對 回復 2023-03-22
?
慕少森

TA貢獻2019條經驗 獲得超9個贊

在這里,總計的值得到更新,但稅金是在檢查之前計算的,如下所示,因此稅金將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()


查看完整回答
反對 回復 2023-03-22
?
嗶嗶one

TA貢獻1854條經驗 獲得超8個贊

我想你的問題已經解決了。也開始使用 python 字典而不是使用大量的 if else 語句


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: "))

            price_dict = {

            1: menu1,

            2: menu2,

            3: menu3,

            4: menu4

            }

          

            if 0 < choose < 5:

                total += price_dict[choose]

            else:

                if 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()

    tax = total*0.06


    print("Current total: $",total)

    print("Sales tax: $",tax)

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

    print("Have a nice day!")

main()


查看完整回答
反對 回復 2023-03-22
  • 3 回答
  • 0 關注
  • 140 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號