简体   繁体   English

Python(pyscripter)

[英]Python (pyscripter)

Following Code is not terminating not sure why. 以下代码未终止并不确定原因。

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
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`

There are 2 reasons that your code did not work. 您的代码无法正常工作的原因有两个。 The first one is that the while loops were not inside the GuessingGame function, causing them not to know what the variables made there were. 第一个是while循环不在GuessingGame函数内部,从而导致他们不知道在那里产生的变量是什么。 The second reason is that if you're going to use a input in a string, you must use raw_input(), not plain input(). 第二个原因是,如果要在字符串中使用输入,则必须使用raw_input()而不是普通的input()。 I hope this helped! 希望对您有所帮助!

I believe your problem is in the line while gameover is 'Yes' or 'yes': GuessingGame () If the user enters yes to if they want to play the game again, you are calling the function over and over again, which is known as recursion. 我相信while gameover is 'Yes' or 'yes': GuessingGame ()您的问题一直存在while gameover is 'Yes' or 'yes': GuessingGame ()如果用户输入yes至他们是否想再次玩游戏,则您将一遍又一遍地调用该函数,这是已知的作为递归。 If I'm correct, this will reset attempts to 0, thus never exiting your while loop. 如果我是正确的,这会将尝试次数重置为0,从而永远不会退出while循环。 I am not sure if this is what you want, but instead of calling GuessingGame(), writing while gameover is 'Yes' or 'yes': continue will actually go back to the top of the while loop. 我不确定这是否是您想要的,而是while gameover is 'Yes' or 'yes': continue编写while gameover is 'Yes' or 'yes': continue不是调用GuessingGame() while gameover is 'Yes' or 'yes': continue实际上将返回while循环的顶部。

Also, I'm not quite sure of what the importance of defining a main() function is, when the body of the function is just pass. 另外,当函数主体刚刚通过时,我不太确定定义main()函数的重要性。 Instead, you should write your GuessinGame() above if name == ' main ', and then have your while loop below if name == ' main ' 相反,你应该写你GuessinGame()以上,如果名称 ==“ ”,然后让你的while循环之下,如果名称 ==“

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM