3 回答

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)景的方法。

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

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")
添加回答
舉報(bào)