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ループは3回の間違った試行の後に終了することはありません.1つは人生が0になり、whileループが終了します。代わりにユーザー入力が整数または単語の場合、事前定義された値と照合するにはどうすればよいですか?
correct_answer = number
の
最初に値が整数かどうかをテストします。 if文では 'if input_variable.isdigit()and ...'や 'if input_varible.isalpha()and ...'のようになります。私は注意する必要がありますが、私はどこにあなたのユーザーの入力を取得しているあなたのプログラムのどこにも表示されません。 –
アドバイスをありがとう、入力は28行目です。 – RetainedFlame7
おそらくロジック全体を修正する必要があります。ライブcoutnerとは別に、forループが大きいため、最初の100回の試行で3回失敗してもプログラムは停止しません。 – josoler