我必须编写一个代码,我需要创建一个让用户猜测方程式的游戏。每次猜测结束后,我必须显示用户猜到了多少方程式。例如,如果等式是 1+2*3+4 并且用户猜测等式有 3,程序会说你的猜测是正确的,到目前为止你猜到的等式是 ----3-- (破折号代表等式有多少个字符。如果用户接下来猜到 2,我需要他们到目前为止猜到的等式是 --2-3-- 但我无法让它们累积。
我正在使用的功能是
def guessing1():
'''
the player is asked to make a guess and the result is printed
'''
wrongguesses=0
if (guess1 in randomFormula):
print "Your guess is correct!"
wrongguesses=0
else:
print "Your guess is wrong!"
wrongguesses +=1
if (wrongguesses== max_guesses):
print "Sorry, you've reached the maximum number of wrong guesses."
print "Better luck next time!"
playagain=raw_input("Do you want to play again? y-yes, n-no: ")
if (playagain== n):
print "The game is over."
def formSoFar1():
a=''
for i in range (len(randomFormula)):
if (randomFormula[i] == guess1):
a += randomFormula[i]
else:
a+= "-"
print "The formula you have guessed so far is: ",a
在调用函数时以任何方式更改此代码,我要么得到错误,要么不使用之前的猜测。我不知道该怎么做。