私は1つの間違った答えの後にもう一度やり直すチャンスを得る乗算プログラムをしようとしていました。Python Multiplication Programゲーム、再試行オプション
私のコードです:問題が再び質問をするとき、それが正しい答えを計算しないで
from random import randint
wrong_answers = 0
for turn in range(100):
# Choose two random integers for the question to the player
factor_1 = randint(2,12)
factor_2 = randint(2,12)
# Precompute the correct answer to be able to check the player's answer
correct_answer = factor_1*factor_2
# As the question and get the player's response
question = 'What is ' + str(factor_1) + ' times ' + str(factor_2) + ' times ' + '?'
answer_string = raw_input(question)
#Convert the player's response to a number (raw_input yields a string)
answer_int = int(answer_string)
# See if the player's answer is correct or not, and proceed accordingly
if (answer_int == correct_answer):
print 'Correct!'
else:
print 'Wrong! Try Again!'
raw_input(question)
answer_int = int(answer_string)
if raw_input() == correct_answer:
print 'Correct'
else:
wrong_answers = wrong_answers + 1
if wrong_answers == 2:
print 'Game Over Thanks for playing!!!'
exit()
...それはそれを無視して、として間違った答えをカウントします。 2回目の間違った答えの後、プログラムは終了する。
もう一度質問をして、正解を数え、間違った答えをエラーとして再度カウントしてプログラムが続行する方法はありますか?
私はこの部分を修正しましたが、今は動作すると思います: else: print '間違っています!再試行する!' answer_string = raw_input() answer_int = int(answer_string) answer_int == correct_answer: 'Correct'i printこの部分を修正しました: –