Python(pyscripter)
以下代码未终止,不确定原因Python(pyscripter),python,pyscripter,Python,Pyscripter,以下代码未终止,不确定原因 def main(): pass if __name__ == '__main__': main() import random # imports random def GuessingGame (): # creating def function for GuessingGame yes = ['y', 'Y', 'Yes', 'yes'] # setting variables rng = random.Random ()
def main():
pass
if __name__ == '__main__':
main()
import random # imports random
def GuessingGame (): # creating def function for GuessingGame
yes = ['y', 'Y', 'Yes', 'yes'] # setting variables
rng = random.Random ()
numbertoguess = rng.randrange (1,10) # setting random number between 1 and 10
correctguesses = 0
winpercent = 0
attempts = 0
name = input('What is your name? ') # asks user for name and introduces the game
print('Nice to meet you', name, '! I am going to pick a random number and you will have 3 tries to guess it.'
'Good luck!')
while attempts < 3: # creates a function
guess = int(input('Guess the number I have chosen between 1 and 10. '))
attempts += 1
if guess == numbertoguess: # tells user if guess is right
print('Great guess, youre right', name, '!')
correctguesses += 1 # adds on to correct guesses
winpercent = float((correctguesses*100)/attempts)# determines percent of correct answers
print ('You have' , correctguesses, 'correct guesses!')
print ('You are right', winpercent, 'of the time.')
elif guess > numbertoguess: # tells if guess is too high
print('The number is lower than', guess, "!")
elif guess < numbertoguess: # tells if guess is too low
print('The number is higher than', guess, "!")
else: # tells when you have lost
print('Wrong, the number was', numbertoguess, '!')
print ('You have' , correctguesses, 'correct guesses!')
print ('You are right', winpercent, 'of the time.')
gameover = input("Do you want to play again? Yes or No? ")
while gameover is 'Yes' or 'yes': GuessingGame () #oportunity to play again
else: exit() # ends games
print ('The program will now terminate. Goodbye.')
GuessingGame () # calls game
我相信你的问题就在gameover为“是”或“是”时:猜游戏如果用户输入Yes to,如果他们想再次玩游戏,你会一遍又一遍地调用该函数,这就是所谓的递归。如果我是正确的,这会将尝试重置为0,从而永远不会退出while循环。我不确定这是否是您想要的,但不是调用GuessingGame,而是在gameover为“是”或“是”时编写:continue实际上将返回while循环的顶部 另外,我不太清楚定义主函数的重要性是什么,当函数的主体是pass时。相反,如果name='main',你应该在上面写下你的猜测名,然后在name='main'下面写下你的while循环
代码不起作用有两个原因。第一个是while循环不在gussinggame函数中,这导致它们不知道那里的变量是什么。第二个原因是,如果要在字符串中使用输入,必须使用原始输入,而不是普通输入。我希望这有帮助 我喜欢python,我不太清楚你在第二段中说的是什么。@我个人喜欢在if name='main'上面写函数,在if name='main'下面调用函数。这让其他人知道我想打什么电话等等。我想问一下main的重要性,因为它的主体只是过客。如果您不知道,您不必每次执行python程序时都定义main当我尝试运行我的程序时,它告诉我未定义全局名称raw_input。我正在用pyscripter运行Python3.3.5,如果这意味着什么的话。您不应该有任何顶级代码。相反,将所有顶级代码插入main,然后将if _uname_u=='_umain:main作为脚本的最后两行。
def main():
pass
if __name__ == '__main__':
main()
import random # imports random
# Your 2 while loops were not indented causing them not to know what the attempt variable, made in the GuessingGame function was.
def GuessingGame (): # creating def function for GuessingGame
yes = ['y', 'Y', 'Yes', 'yes'] # setting variables
rng = random.Random ()
numbertoguess = rng.randrange (1,10) # setting random number between 1 and 10
correctguesses = 0
winpercent = 0
attempts = 0
name = raw_input('What is your name? ') # asks user for name and introduces the game (inputs used in strings must be raw.)
print('Nice to meet you', name ,'! I am going to pick a random number and you will have 3 tries to guess it.'
'Good luck!')
while attempts < 3: # creates a function
guess = int(input('Guess the number I have chosen between 1 and 10. '))
attempts += 1
if guess == numbertoguess: # tells user if guess is right
print('Great guess, youre right', name, '!')
correctguesses += 1 # adds on to correct guesses
winpercent = float((correctguesses*100)/attempts)# determines percent of correct answers
print ('You have' , correctguesses, 'correct guesses!')
print ('You are right', winpercent, 'of the time.')
elif guess > numbertoguess: # tells if guess is too high
print('The number is lower than', guess, "!")
elif guess < numbertoguess: # tells if guess is too low
print('The number is higher than', guess, "!")
else: # tells when you have lost
print('Wrong, the number was', numbertoguess, '!')
print ('You have' , correctguesses, 'correct guesses!')
print ('You are right', winpercent, 'of the time.')
gameover = raw_input("Do you want to play again? Yes or No? ")
while gameover is 'Yes' or 'yes': GuessingGame () #oportunity to play again
else: exit() # ends games
print ('The program will now terminate. Goodbye.')
GuessingGame () # calls game`enter code here`