0
我試圖實現自定義驗證......我的目標是,如果用戶的回覆不是「Y」,或不是「N」,或不是「Q」,他們循環回到頂部。否則打破。
在Python中自定義驗證
我試過的兩個選項都會繼續循環,即使給出了正確的響應。
這是我已經試過: 選項1:
""" Use custom validation. """
while True:
n_put = input('Would you like to perform a new Google image search?' + user_options())
if n_put is not "Y" or not "N" or not "Q":
print('Invalid response. Please read the prompt carefully. ')
else:
break
選項2:
""" Use custom validation. """
while True:
n_put = input('Would you like to perform a new Google image search?' + user_options())
if n_put is not any(["Y", "N", "Q"]):
print('Invalid response. Please read the prompt carefully. ')
else:
break
'如果不是n_put in ['Y','N','Q']:'或'如果不是n_put =='Y'而不是n_put =='N'' ... – cwallenpoole
可能重複[請求用戶輸入,直到他們給出有效的響應爲止(https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – wwii
@wwii ,我的問題涉及多個非條件。沒有太多的循環。 –