我目前正在使用learnpython.org进行python练习: 我在加粗的额外例外方面遇到了麻烦:
生成1到9之间的随机数(包括1和9)。让用户猜出数字,然后告诉他们他们是否猜到太低,太高或完全正确。 (提示:记得使用第一次练习中的用户输入课程)
附加功能:
让游戏继续进行,直到用户输入“退出”
跟踪用户拍摄的猜测次数,以及游戏何时结束,打印出来。
到目前为止,这是我的代码:
import random
def gameTracker():
global playedGames
playedGames = 1
playedGames = playedGames+1
def generateNumber():
global generatednumber
generatednumber = random.randint(1, 9)
def generateuserGuess():
global userguess
userguess = int(input('Pick a number between 1-9: '))
def generationProcess():
global userguess
if int(userguess) == generatednumber:
print('You have guessed the number. Congratulations.')
gameTracker()
global playAgain
playAgain = input('Want to play again? y/n: ')
if playAgain == 'y':
generateNumber()
userguess = input('Pick a new number between 1-9: ')
generationProcess()
if playAgain == 'n':
print('The game has ended. You have played: ')
print(int(playedGames))
print('games')
if int(userguess) > generatednumber:
print('You have guessed too high.')
global conConfirm
conConfirm = str(input('Guess again?: c/quit: '))
conGame()
if int(userguess) < generatednumber:
print('You have guessed too low.')
conConfirm = str(input('Guess again?: c/quit: '))
conGame()
def conGame():
if conConfirm == 'c':
global userguess
userguess = int(input('Your new guess please: '))
generationProcess()
if conConfirm == 'quit':
print('The game has ended. You have played: ')
print(int(playedGames))
print('games')
generateNumber()
generateuserGuess()
generationProcess()
当用户按下&#39; n&#39;或者&#39;退出&#39;在播放之后它并没有打印出正确数量的游戏,这意味着gameTracker()
被写错了,但也出于某种原因打印了游戏玩过的次数*之后会发生什么playAgain ==&#39; n&#39;。
我将如何开展这项工作?
答案 0 :(得分:0)
在gameTracker()
代码中,将playedGames
初始化为1,然后将其递增。所以它看起来总是存储值2.
由于递归,您会得到多个输出。在最后一次递归调用之后,程序控制返回到if语句,变量的值仍为'n'或'quit'将再次打印详细信息。您可以在退出条件之后将变量初始化为不同的值以避免这种情况。
import random
playedGames = 0
def gameTracker():
global playedGames
playedGames = playedGames+1
def generateNumber():
global generatednumber
generatednumber = random.randint(1, 9)
gameTracker()
def generateuserGuess():
global userguess
userguess = int(input('Pick a number between 1-9: '))
def generationProcess():
global userguess
global generatednumber
if int(userguess) == generatednumber:
print('You have guessed the number. Congratulations.')
global playAgain
playAgain = input('Want to play again? y/n: ')
if playAgain == 'y':
generateNumber()
userguess = input('Pick a new number between 1-9: ')
generationProcess()
if playAgain == 'n':
print('The game has ended. You have played: ')
print(int(playedGames))
print('games')
playAgain = 'x'
if int(userguess) > generatednumber:
print('You have guessed too high.')
global conConfirm
conConfirm = input('Guess again?: c/quit: ')
conGame()
if int(userguess) < generatednumber:
print('You have guessed too low.')
conConfirm = str(input('Guess again?: c/quit: '))
conGame()
def conGame():
global conConfirm
if conConfirm == 'c':
global userguess
userguess = int(input('Your new guess please: '))
generationProcess()
if conConfirm == 'quit':
print('The game has ended. You have played: ')
print(int(playedGames))
print('games')
conConfirm = 'x'
generateNumber()
generateuserGuess()
generationProcess()
答案 1 :(得分:0)
在generateNumber()
方法中声明后,您应该在gameTracker()
方法中增加游戏次数。通过这种方式,您可以跟踪已播放的游戏数量,无论玩多少游戏!它看起来像这样:
def gameTracker():
global playedGames
playedGames = 0
def generateNumber():
global generatednumber
generatednumber = random.randint(1, 9)
playedGames += 1
答案 2 :(得分:0)
与globals
和function returns
合作。我调整了代码中的一些错误并添加了一些注释。看看!
import random
#create variable for num of played games
playedGames = 0
#creat variable to count number of guesses per game
guesses = 0
#create function to generate new random num
def generateNumber():
return random.randint(1, 9)
#askk initial question
generatednumber = generateNumber()
userguess = int(input('Pick a number between 1-9: '))
def generationProcess():
global userguess, guesses, playedGames,generatednumber
while True:
if userguess == generatednumber:
print('You have guessed the number. Congratulations.')
playedGames += 1
global playAgain
playAgain = input('Want to play again? y/n: ')
if playAgain == 'y':
generatednumber = generateNumber()
userguess = input('Pick a new number between 1-9: ')
generationProcess()
if playAgain == 'n':
print('The game has ended. You have played: '+ str(playedGames)+' games')
elif int(userguess) > generatednumber:
print('You have guessed too high.')
guesses+=1
conConfirm = str(input('Guess again?: c/quit: '))
conGame(conConfirm)
elif int(userguess) < generatednumber:
guesses+=1
print('You have guessed too low.')
conConfirm = str(input('Guess again?: c/quit: '))
conGame(conConfirm)
def conGame(conConfirm):
global userguess
if conConfirm == 'c':
userguess = int(input('Your new guess please: '))
generationProcess()
if conConfirm == 'quit':
print('The game has ended. You have played: ' + str(playedGames) + ' games')
generationProcess()