簡體   English   中英

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

您的代碼無法正常工作的原因有兩個。 第一個是while循環不在GuessingGame函數內部,從而導致他們不知道在那里產生的變量是什么。 第二個原因是,如果要在字符串中使用輸入,則必須使用raw_input()而不是普通的input()。 希望對您有所幫助!

我相信while gameover is 'Yes' or 'yes': GuessingGame ()您的問題一直存在while gameover is 'Yes' or 'yes': GuessingGame ()如果用戶輸入yes至他們是否想再次玩游戲,則您將一遍又一遍地調用該函數,這是已知的作為遞歸。 如果我是正確的,這會將嘗試次數重置為0,從而永遠不會退出while循環。 我不確定這是否是您想要的,而是while gameover is 'Yes' or 'yes': continue編寫while gameover is 'Yes' or 'yes': continue不是調用GuessingGame() while gameover is 'Yes' or 'yes': continue實際上將返回while循環的頂部。

另外,當函數主體剛剛通過時,我不太確定定義main()函數的重要性。 相反,你應該寫你GuessinGame()以上,如果名稱 ==“ ”,然后讓你的while循環之下,如果名稱 ==“

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM