0
import random
lives = 3
while lives > 0:
print("If the number is divisible by 3, type FIZZ (all caps)")
print("If the number is divisible by 5, type BUZZ (all caps)")
print("If the number is divisible by 3 and 5, type FIZZ BUZZ (all caps)")
print("If the number is none of the above, type the number")
print("You have three lives and then the game is over")
print("Try and beat me!")
for number in range(1,101):
if number % 3 == 0 and number % 5 == 0:
correct_answer = "FIZZ BUZZ"
elif number % 3 == 0:
correct_answer = "FIZZ"
elif number % 5 == 0:
correct_answer = "BUZZ"
else:
correct_answer = number
first_go = random.randint(0,1)
if first_go == 0:
computer_go = True
else:
computer_go = False
if computer_go == True:
print("CPU:", correct_answer)
computer_go == False
elif computer_go == False:
answer = input("Your go:")
if answer == correct_answer:
computer_go = True
else:
print("Wrong answer!")
lives - 1
我試圖使用if語句'if answer == correct_answer',但它似乎不適用於既不是FIZZ,BUZZ也不是FIZZ BUZZ的值。我在while循環方面也遇到了麻煩; 「錯誤答案」被打印出來,然而while循環在三次錯誤的嘗試之後並沒有結束,因爲它應該與生命有關-1導致生命變爲0並結束while循環。如何根據預先定義的值檢查用戶輸入是否可以是整數或單詞?
首先測試該值是一個整數還是一個單詞。在if語句中,如'if input_variable.isdigit()and ...'或'if input_varible.isalpha()and ...'。雖然我應該注意到,我沒有看到您的程序中的任何地方獲得用戶輸入。 –
謝謝你的建議,輸入是在第28行。 – RetainedFlame7
您可能需要修改整個邏輯。除了現場服裝之外,即使你在前100次嘗試中失敗三次,程序也不會停止,因爲循環較大。 – josoler