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

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

Python:確保字符串是用戶的有效輸入,并拒絕任何其他輸入

Python:確保字符串是用戶的有效輸入,并拒絕任何其他輸入

慕俠2389804 2024-01-15 21:09:49
嘗試配置我的程序,使其僅在整個(gè)程序中接受“是”和“否”的答案,并且任何其他輸入(例如整數(shù)和其他字符串)都會(huì)產(chǎn)生錯(cuò)誤消息。使用 .isalpha() 也不適合我,任何有關(guān)如何幫助編寫代碼的提示將不勝感激。text = True    if text:        textHelp = input('Are you sleeping? '.isalpha())        if textHelp == 'yes':            print('Cannot take phone call b/c im sleeping')        else:            print('Can take phone call)
查看完整描述

3 回答

?
慕工程0101907

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

根據(jù)您的描述,您要尋找的是這樣的:


input_text = input('Are you sleeping? ').lower()


if input_text == 'yes':

  print('Cannot take phone call b/c im sleeping')

elif input_text == 'no':

  print('Can take phone call')

else:

  print('Expected a yes or a no for an answer')

Thr.lower()方法確?!癥es”、“yes”、“yEs”等得到相同的處理。您不需要.isalpha()此場(chǎng)景的方法。


查看完整回答
反對(duì) 回復(fù) 2024-01-15
?
楊魅力

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

我想這就是您正在尋找的。


while true:

? ? textHelp = input('Are you sleeping? ')

? ? if textHelp.isalpha():

? ? ? ? # your expected output ...

? ? ? ? break

? ? else:

? ? ? ? print("try again!")


查看完整回答
反對(duì) 回復(fù) 2024-01-15
?
慕無(wú)忌1623718

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

您可以嘗試使用 if..elif..else:


if textHelp == 'yes':

    print('Cannot take phone call b/c im sleeping')

elif textHelp == 'no':

    print('Can take phone call')

else:

   print("Faulty message")

如果您想在無(wú)效輸入時(shí)保持提示繼續(xù):


while True:

  if textHelp == 'yes':

    print('Cannot take phone call b/c im sleeping')

    break

  elif textHelp == 'no':

    print('Can take phone call')

    break

  else:

   print("Faulty message")


查看完整回答
反對(duì) 回復(fù) 2024-01-15
  • 3 回答
  • 0 關(guān)注
  • 182 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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