簡體   English   中英

我在這個猜數字游戲中做錯了什么?

[英]What am I doing wrong with this number guessing game?

我正在做一個數字猜謎游戲,4天后我感到非常困惑。 我修復了一些東西,然后出現了另一個問題。 在這一點上它是一團糟。 我知道在這里問模糊的問題是不好的做法,但我很絕望。 我的項目將在 6 小時內到期,除了你們之外,還有誰可以求助於 IDK。 任何不給我解決方案的幫助都會很棒。

import random
game_history = open('gameHistory.txt', 'w')

def main():
    game_instructions()
    start_game()

def start_game():
    game_history = open('gameHistory.txt', 'w')
    flag = True
    while flag:
        lives = 0        secret_number = random.randint(1, 30)
        print(secret_number)
        guess_list=[]
          
        proceed = True
        while proceed:        
            print('Enter a number: ')     
            while lives < 5 and proceed:
                try:    
                    user_guess = int(input())
                    guess_list.append(user_guess)
                    if user_guess == secret_number:
                        game_history.write('w' + '\n')
                        print('\n', 'You win.')
                        keep_going = input('Try again? Enter y for yes. Anything else for no: ')
                        if keep_going == 'y':
                            proceed = False
                            game_instructions()
                            start_game()
                        else:
                            end_game()
                            proceed = False
                            flag = False

                    elif user_guess > secret_number:
                        print('Nope. Lower.')
                        lives += 1
                        print('Attempts: ', lives)

                    elif user_guess < secret_number:
                        print('Nope. Higher.')
                        lives += 1
                        print('Attempts: ', lives)
                   
          

                    if lives == 5:
                        game_history.write('l' + '\n')
                        print('Game over. Try again?', '\n', 'Correct number is: ',secret_number, '\n', 'Your guesses: ', guess_list)
                        keep_going = input('enter y for yes: ')
                        if keep_going == 'y':
                            proceed = False
                            flag = False
                            game_instructions()
                            start_game()
                        else:
                            end_game()
                            proceed = False
                            flag = False
                            
                             
                except ValueError:
                    print('Please enter a number.')
    flag = False
    proceed = False
   
    game_history.close()
   
            
        
def end_game():
    print('Game Over. Goodbye.')
    
def game_instructions():
    print('''
===========================================
*******************************************
Number guessing game!
*******************************************

This game gives you 5 attemtps at
guessing a randomly generated number
in the range of and including 1 to 30.
*******************************************

Directions:
Enter a whole number between and
including 1 and 30.
You have 5 lives! Good luck!
*******************************************
===========================================
''')

# Call main function to start program
main()    

太多的while循環和函數調用,但我像你一樣重做了你的過程。 我認為這會有所幫助。

import random
import sys

def main():
    game_instructions()
    start_game()


def start_game():
    game_history = open('gameHistory.txt', 'a')
    secret_number = random.randint(1, 30)
    print(secret_number)
    lives = 0
    guess_list = []
while True:
    
    print('Enter a number: ')

    try:
        user_guess = int(input())
        guess_list.append(user_guess)
        if user_guess == secret_number:
            game_history.writelines("win" + "\n")
            print('\n', 'You win.')
            keep_going = input('Try again? Enter y for yes. Anything else for no: ')
            print(keep_going)
            if keep_going == 'y':
                main()
            else:
                game_history.close()
                end_game()

        elif user_guess > secret_number:
            print('Nope. Lower.')
            lives += 1
            print('Attempts: ', lives)

        elif user_guess < secret_number:
            print('Nope. Higher.')
            lives += 1
            print('Attempts: ', lives)

        if lives == 5:
            game_history.writelines('lose' + '\n')
            print('Game over. Try again?', '\n', 'Correct number is: ', secret_number, '\n',
                  'Your guesses: ', guess_list)
            keep_going = input('enter y for yes: ')
            if keep_going == 'y':
               main()
            else:
                game_history.close()
                end_game()


    except ValueError:
        print('Please enter a number.')




def end_game():
    print('Game Over. Goodbye.')
    sys.exit(0)



def game_instructions():
    print('''
===========================================
*******************************************
Number guessing game!
*******************************************

This game gives you 5 attemtps at
guessing a randomly generated number
in the range of and including 1 to 30.
*******************************************

Directions:
Enter a whole number between and
including 1 and 30.
You have 5 lives! Good luck!
*******************************************
===========================================
''')


# Call main function to start program
main()`

暫無
暫無

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

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