只是快速了解一下它的作用。生成一个由数字和 2 个符号(+ 和 *)组成的秘密字符串,供用户猜测。我猜是刽子手。我已经解决了我遇到的所有其他问题,而且我只剩下一个半了。我是新手。
首先,一个问题是有时运行它(在 IDLE 中使用 F5)不会显示任何内容。只需“>>”,我必须关闭外壳并重新运行它。
其次,在赢得并拒绝再次比赛的提议后,它仍然要求输入猜测。我想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()