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

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何運(yùn)行python循環(huán)直到輸入正確的輸入字符串

如何運(yùn)行python循環(huán)直到輸入正確的輸入字符串

躍然一笑 2023-09-12 17:51:57
我必須編寫一個(gè)代碼,僅當(dāng)用戶輸入術(shù)語“Cyril”時(shí)才會(huì)執(zhí)行下面的 while 循環(huán)。我是一個(gè)真正的新手,我只能想出以下解決方案,該解決方案將強(qiáng)制用戶重新啟動(dòng)程序,直到他們輸入正確的輸入,但我希望它繼續(xù)要求用戶輸入,直到他們輸入正確答案。有人可以幫忙嗎?我知道一旦我意識(shí)到有一個(gè)簡單的解決方案,我可能會(huì)踢自己。number_list = []attempts = 0name = Falsenumber = 0name_question = input("You are the President of RSA, what is your name?: ")if name_question == "Cyril":    name = Trueelse:    print("\nThat is incorrect, please restart the program and try again.\n")  if name:    number = int(input("Correct! Please enter any number between -1 and 10: "))    while number > -1:        number_list.append(number)        number = int(input("\nThank you. Please enter another number between -1 and 10: "))    if number > 10:        print("\nYou have entered a number outside of the range, please try again.\n")        number = int(input("Please enter a number between -1 and 10: "))    elif number < -1:        print("\nYou have entered a number outside of the range, please try again. \n")        number = int(input("Please enter a number between -1 and 10: "))    elif number == -1:        average_number = sum(number_list) / len(number_list)        print("\nThe average number you have entered is:", round(average_number, 0))
查看完整描述

2 回答

?
神不在的星期二

TA貢獻(xiàn)1963條經(jīng)驗(yàn) 獲得超6個(gè)贊

將代碼開頭更改為:


while True:

    name_question = input("You are the President of RSA, what is your name?: ")

    if name_question == "Cyril":

        name = True

        break

    else:

        print("\nThat is incorrect, please try again.\n") 


查看完整回答
反對 回復(fù) 2023-09-12
?
GCT1015

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超4個(gè)贊

即使我不明白你的問題是否簡單或者我是個(gè)白癡,你也可以嘗試這個(gè):


name_question = input("You are the President of RSA, what is your name?: ")

while name_question != "Cyril":

    name_question = input("You are the President of RSA, what is your name?: ")

...


查看完整回答
反對 回復(fù) 2023-09-12
  • 2 回答
  • 0 關(guān)注
  • 154 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號