python 猜谜游戏练习
python guessing game excersise
我目前正在做 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()
当用户在玩完后按'n'或'quit'时,它不会打印出正确的游戏数,这意味着gameTracker()
被写成badly/wrong,但是由于某种原因,它还会打印游戏的播放次数 * playAgain == 'n'.
之后会发生什么
我将如何完成这项工作?
在您的 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()
你应该在 gameTracker()
方法中声明后增加 generateNumber()
方法中玩的游戏数量。这样,无论玩了多少游戏,您都可以跟踪玩了多少游戏!它看起来像这样:
def gameTracker():
global playedGames
playedGames = 0
def generateNumber():
global generatednumber
generatednumber = random.randint(1, 9)
playedGames += 1
与您的 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()
我目前正在做 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()
当用户在玩完后按'n'或'quit'时,它不会打印出正确的游戏数,这意味着gameTracker()
被写成badly/wrong,但是由于某种原因,它还会打印游戏的播放次数 * playAgain == 'n'.
我将如何完成这项工作?
在您的 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()
你应该在 gameTracker()
方法中声明后增加 generateNumber()
方法中玩的游戏数量。这样,无论玩了多少游戏,您都可以跟踪玩了多少游戏!它看起来像这样:
def gameTracker():
global playedGames
playedGames = 0
def generateNumber():
global generatednumber
generatednumber = random.randint(1, 9)
playedGames += 1
与您的 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()