-1

Im working on a number guessing game and 4 days into it I'm beyond confused. I fix something and another problem appears. At this point its a mess. I know it's bad practice asking vague question on here but I'm desperate. My project is due in 6 hours and IDK who to turn to besides you guys. Any help without giving me the solution would be amazing.

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()    
  • 1
    What is your problem exactly? What is not working? – jacky la mouette Aug 18 '22 at 13:40
  • 1
    [Under what circumstances may I add "urgent" or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) – Klaus D. Aug 18 '22 at 13:43
  • There are some design flaws such as 1) having the start_game call itself and 2) opening game history globally and locally. But, what isn't working? Also, since this is a popular question on StackOverflow you could gain some insight from seeing how others approach this problem such as [Number Guessing Game](https://stackoverflow.com/questions/53038112/number-guessing-game-python/53038153#53038153) – DarrylG Aug 18 '22 at 13:53
  • you have 3 nested while loops, the outer two only ever complete one loop. You are calling `start_game()` recursively, instead of just breaking the inner loop. because of that, you open the game history every time, and never close it... – Jeanot Zubler Aug 18 '22 at 14:07
  • What happens is I run the game and win and restart from the keep_going. The i enter 5 incorrect inputs, then im prompted to enter y for restart or anything else to end the game. When anything else is entered an infinite loop starts with the 'Enter a number. – Just a person Aug 18 '22 at 14:08
  • I'm also not supposed to use the break function – Just a person Aug 18 '22 at 14:08
  • perhaps try to separate out things into methods. something like `def play_single_game()` this method would then loop through the lives/attempts, and return True as soon as the game is won, otherwise false. then in a method called `play()` you initialize values (open the history, ...) and then make a loop `while wants_to_play:` in this you call `bool win = play_single_game()` and can then ask the player if he wants to continue. if not, set `wants_to_play = False` – Jeanot Zubler Aug 18 '22 at 14:21

1 Answers1

0

Too many while loops and functions calls but I redone your process like you did. I think this will help.

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()`
Guest
  • 1
  • 1