首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python (脚本编写器)

Python (脚本编写器)
EN

Stack Overflow用户
提问于 2015-06-08 08:57:51
回答 2查看 552关注 0票数 0

下面的代码不是终止,不确定原因。

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

发布于 2015-06-08 09:33:02

代码语言:javascript
复制
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`

有两个原因导致你的代码不能工作。第一个问题是while循环不在GuessingGame函数中,导致它们不知道那里的变量是什么。第二个原因是,如果要在字符串中使用输入,则必须使用raw_input(),而不是普通的input()。我希望这对你有帮助!

票数 0
EN

Stack Overflow用户

发布于 2015-06-08 09:07:53

我相信你的问题出在while gameover is 'Yes' or 'yes': GuessingGame ()这一行,如果用户输入yes,如果他们想再次玩游戏,你就会一遍又一遍地调用这个函数,这就是所谓的递归。如果我是正确的,这会将尝试次数重置为0,因此永远不会退出while循环。我不确定这是否是您想要的,但是不是调用GuessingGame(),而是编写while gameover is 'Yes' or 'yes': continue实际上会回到while循环的顶部。

此外,我也不太确定定义main()函数的重要性,因为函数的主体只是传递。相反,如果name == 'main',则应该在上面编写GuessinGame(),如果name == 'main‘,则应该在下面进行循环。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30699973

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档