gpt4 book ai didi

python - 字符串在不应该的情况下发生变化&运行问题

转载 作者:太空宇宙 更新时间:2023-11-03 19:09:55 24 4
gpt4 key购买 nike

快速概述一下它的作用。生成由用户猜测的数字和 2 个符号(+ 和 *)组成的 secret 字符串。我猜是刽子手。我已经解决了所有其他问题,现在只剩一个半了。我是新手。

首先,一个问题是有时运行它(在 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()

最佳答案

这段代码最大的问题是你到处使用全局变量;如果您将值存储在 python 对象中并在每次开始新游戏时初始化您的对象,效果会更好。

要修复您现在使用的内容,您需要在 play_again() 函数中初始化 letters...

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()

关于python - 字符串在不应该的情况下发生变化&运行问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13328763/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com