第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

當變量值不匹配時如何阻止python運行某些'if'代碼

當變量值不匹配時如何阻止python運行某些'if'代碼

一只斗牛犬 2022-05-19 18:38:03
嘗試在 python 2.7 中創(chuàng)建決策菜單。不能讓選項是個人的,而不是按順序排列的。除了if給出語法錯誤之外的任何東西pc = 英鎊換算kc = 千克換算cont = 1while cont == 1:    if input("Would you like to convert to pounds or kilograms?") == 'pounds':        pc = 1    if pc == 1:        kilograms = float(input("Enter the amount of kilograms:  "))        pounds = kilograms / 2.205        print('The amount of pounds you entered is ', kilograms,                  ' This is ', pounds, ' pounds ')        pc = 0        if input('Do you want to go again? (y/n) ') == 'n':            cont = 0    if input("Would you like to convert to pounds or kilograms?") == "kilograms":        kc = 1    if kc == 1:        pounds = float(input("Enter the amount of pounds:  "))        kilograms = pounds * 2.2        grams = kilograms * 1000        print('The amount of pounds you entered is ', pounds,                  ' This is ', kilograms, ' kilograms ', 'and', grams,                  'grams' )        kc = 0    if input('Do you want to go again? (y/n) ') == 'n':        cont = 0在kilograms第一次提示時輸入“PC未定義”并且程序只有在您輸入poundsfirst, kilogramssecond時才能正常運行
查看完整描述

2 回答

?
斯蒂芬大帝

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

while True: #generic while statement, only way to exit is break

    choice = input("Would you like to convert to pounds or kilograms: ") #store our choice in a variable for later

    if choice == 'pounds':

        kilograms = float(input("Enter the amount of kilograms:  "))

        pounds = kilograms / 2.205

        print('The amount of killograms you entered is ', kilograms,

                  ' This is ', pounds, ' pounds ')


    elif choice == "kilograms": #use an elif and define a different specific statement

        pounds = float(input("Enter the amount of pounds:  "))

        kilograms = pounds * 2.2

        grams = kilograms * 1000


        print('The amount of pounds you entered is ', pounds,

                  ' This is ', kilograms, ' kilograms ', 'and', grams,

                  'grams' )


    elif choice == "quit":

        print("Goodbye!")

        break #use break to break out of a loop


    else: #use an else to define a generic statement

        print "%s is not a valid choice"%choice

        continue #use continue to return to the beginning of the loop without doing anything after


    do_continue = input('Do you want to go again? (y/n) ')

    if do_continue == 'y':

        pass #do nothing and continue


    elif do_continue == 'n':

        print("Goodbye!")

        break


    else:

        print("What language is that? We will go again anyways!")


查看完整回答
反對 回復 2022-05-19
?
慕萊塢森

TA貢獻1810條經驗 獲得超4個贊

您的代碼中有一個“錯誤”。

該變量pc僅在第一次提示時才定義 == 'pounds'。因此,您會pc is not defined在第 7 行收到錯誤消息,因為它僅在您輸入“磅”時才被定義。


查看完整回答
反對 回復 2022-05-19
  • 2 回答
  • 0 關注
  • 197 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

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

公眾號

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