0
我想弄清楚爲什麼任何以'y'開頭的輸入都會讓這個遊戲繼續運行....看着它只出現'y'或'yes'的代碼應該保持它運行,但是我可以輸入'yensdg','yyyy'等,它仍然會循環。骰子模擬器輸入 - Python
任何想法?由於
from random import randint
repeat = True
while repeat:
print('You rolled', randint(1,6))
print('Do you want to roll again?')
repeat = ('y' or 'yes') in input().lower()
@vaultah如果我有一美元每次有人得到了與抓在python中失敗的僞自然語言語法... –
input()。lower()中的'y',如果輸入字符串中的任何位置存在'y',則此語句將返回true。就像輸入是'thisisarandominputwithy'一樣,它會返回true,因爲它最後有'y'。 –
將最後一行更改爲:repeat = input()。lower()in ['y','yes'] –