只是快速了解这一点。生成一个秘密的数字串和2个符号(+和*),供用户猜测。我想是刽子手。我已经解决了我遇到的所有其他问题,而且我已经解决了一半。我是新手。
首先,一个问题是,有时运行它(在IDLE中使用F5)将不会显示任何内容。只需“>>”,我就必须关闭shell并重新运行它。
其次,在赢得并拒绝再次播放的提议后,仍然要求输入猜测。我想while循环仍然会运行,所以我怎么能阻止它呢?
可能还有其他一些我忽略的问题,但我已经完成了一些,似乎一切正常。如果有任何建议可以让它变得更好(但没有太高级),请随时指出它!
所以这就是我所拥有的:
from random import randint, choice
maxguesses = 11
wins = 0
losses = 0
points = 0
games = 0
numguesses = 0
allowed = ('0','1','2','3','4','5','6','7','8','9','0','+','*')
guesses = ''
def genform():
global secret
global guesses
global maxguesses
sym = choice(['++','**','+*','*+'])
num = ''
max = randint(3,9)
for i in range(max):
num = num + str(randint(0,9))
secret = sym + num
guesses = ''
maxguesses = len(secret)+2
return secret
def evaluate(b):
global evaluated
evaluated = ''
s1 = secret[0]
s2 = secret[1]
for i in range(2,len(secret)-1):
evaluated = evaluated + secret[i]
if (i%2 == 0):
evaluated = evaluated + s1
else:
evaluated = evaluated + s2
evaluated = evaluated + secret[-1]
return evaluated
letters = set()
partial = ''
def takeguess(ch):
global numguesses, points, games, wins, partial, guesses
numguesses = numguesses + 1
print 'Remaining Guesses: ' + str(maxguesses-numguesses)
if (ch in partial) or (ch in guesses):
print "\nYou've already guessed '%s'. Try again: " % ch
elif ch not in secret:
guesses = guesses + ch
print 'Wrong, guess again.'
elif ch in secret:
letters.add(ch)
print "\nGood guess, it's in the secret formula!"
partial = ''.join([l if l in letters else '-' for l in secret])
print 'Formula so far: ' + partial
if partial == secret:
print 'You win!'
wins = wins + 1
points = points + 2
games = games + 1
bonus = raw_input("Evaluate the formula for 10 bonus points: ")
if bonus == str(eval(evaluate(secret))):
points = points + 10
print "That's correct! +10 points."
print eval(evaluate(secret)) #REMOVE AFTER
play_again()
else:
print "That's incorrect. The right answer is", eval(evaluate(secret))
play_again()
def play_again():
global numguesses, guesses, partial, letters
letters = set()
if points < 2:
print "\nYou don't have enough points to play again.\nGames Played: " + str(games) + '\nPoints: ' + str(points)
else:
ans = raw_input('Would you like to play again? [y/n] ')
if ans in ('yY'):
numguesses = 0
guesses = ''
partial = ''
play()
elif ans in ('nN'):
print '\nOkay, goodbye.\nWins: ' + str(wins) + '\nLosses: ' + str(losses) + '\nPoints: ' + str(points)
else:
print 'Invalid input.'
play_again()
def play():
global numguesses, guesses, partial, points, games, losses
genform()
print 'Unsolved formula: ' + ('-'*len(secret))
print 'You have ' + str(maxguesses) + ' guesses.'
print secret #REMOVE AFTER
while (numguesses < maxguesses):
guess = raw_input('\nEnter a guess: ') # Receive guess
if guess not in allowed: # Check if valid
print '\nInvalid guess, please enter a single digit, *, or +. Try again: '
elif partial != secret:
takeguess(guess)
print guesses # REMOVE AFTER
if numguesses == maxguesses:
points = points - 2
games = games + 1
losses = losses + 1
print 'Sorry, you lose. The answer is: ' + secret
play_again()
return
print 'Welcome to hangman!\n'
play()
答案 0 :(得分:1)
这段代码最大的问题是你到处都在使用全局变量;如果你在python对象中存储值并在每次开始新游戏时初始化你的对象,这会好得多。
要修复您现在使用的内容,您需要在letters
函数中初始化play_again()
...
def play_again():
global numguesses, guesses, partial
global letters
letters=set() # Reset letters to be empty
if points < 2:
print "\nYou don't have enough points to play again.\nGames Played: " + str(games) + '\nPoints: ' + str(points)
else:
ans = raw_input('Would you like to play again? [y/n] ')
if ans in ('yY'):
numguesses = 0
guesses = ''
partial = ''
play()
elif ans in ('nN'):
print '\nOkay, goodbye.\nWins: ' + str(wins) + '\nLosses: ' + str(losses) + '\nPoints: ' + str(points)
else:
print 'Invalid input.'
play_again()