Search code examples
pythonpython-3.xfunctionloopsproject

Why does my number guessing game keep looping even after the right guess is made?


The program is a Number guessing Game. The program doesn't stop executing after all 5 guesses are executed. What is the bug in the code?

#importing modules
import random

#ASCII Art Logo
logo="""
█▀▀▀ █──█ █▀▀ █▀▀ █▀▀   ▀▀█▀▀ █──█ █▀▀   █▀▀▄ █──█ █▀▄▀█ █▀▀▄ █▀▀ █▀▀█ 
█─▀█ █──█ █▀▀ ▀▀█ ▀▀█   ──█── █▀▀█ █▀▀   █──█ █──█ █─▀─█ █▀▀▄ █▀▀ █▄▄▀ 
▀▀▀▀ ─▀▀▀ ▀▀▀ ▀▀▀ ▀▀▀   ──▀── ▀──▀ ▀▀▀   ▀──▀ ─▀▀▀ ▀───▀ ▀▀▀─ ▀▀▀ ▀─▀▀"""
print(logo)
print("Welcome to the Number Guessing Game!")
choices_list=[]
for i in range (0,100):
  choices_list.append(i+1)
chosen_number=random.choices(choices_list)[0]
print(chosen_number)
print("I'm thinking of a number between 1 and 100. \n")
game_level=input("Choose a difficulty. Type 'easy' or 'hard'.").lower()
if game_level=='easy':
  attempts=10
elif game_level=='hard':
  attempts=5

#function that enables player to make a guess
def user_choice(attempts):
  print(f"You have {attempts} attempts left to guess the number.")
  choice=int(input("Make a guess: "))
  if choice<chosen_number:
    print("Too Low")
    attempts-=1
    while attempts>0:
      user_choice(attempts)
  elif choice>chosen_number:
    print("Too High.")
    attempts-=1
    while attempts>0:
      user_choice(attempts)
  elif choice==chosen_number:
    print("Congratulations! You made the right guess!")
  
  
    

user_choice(attempts)

I tried to insert break statements but they caused syntax errors.


Solution

  • i found that you could just change the while to if for both of the whiles and it will run just fine. so the code would be:

    #importing modules
    import random
    
    #ASCII Art Logo
    logo="""
    █▀▀▀ █──█ █▀▀ █▀▀ █▀▀   ▀▀█▀▀ █──█ █▀▀   █▀▀▄ █──█ █▀▄▀█ █▀▀▄ █▀▀ █▀▀█ 
    █─▀█ █──█ █▀▀ ▀▀█ ▀▀█   ──█── █▀▀█ █▀▀   █──█ █──█ █─▀─█ █▀▀▄ █▀▀ █▄▄▀ 
    ▀▀▀▀ ─▀▀▀ ▀▀▀ ▀▀▀ ▀▀▀   ──▀── ▀──▀ ▀▀▀   ▀──▀ ─▀▀▀ ▀───▀ ▀▀▀─ ▀▀▀ ▀─▀▀"""
    print(logo)
    print("Welcome to the Number Guessing Game!")
    choices_list=[]
    for i in range (0,100):
      choices_list.append(i+1)
    chosen_number=random.choices(choices_list)[0]
    print(chosen_number)
    print("I'm thinking of a number between 1 and 100. \n")
    game_level=input("Choose a difficulty. Type 'easy' or 'hard'.").lower()
    if game_level=='easy':
      attempts=10
    elif game_level=='hard':
      attempts=5
    
    #function that enables player to make a guess
    def user_choice(attempts):
      print(f"You have {attempts} attempts left to guess the number.")
      choice=int(input("Make a guess: "))
      if choice<chosen_number:
        print("Too Low")
        attempts-=1
        if attempts>0:
          user_choice(attempts)
      elif choice>chosen_number:
        print("Too High.")
        attempts-=1
        if attempts>0:
          user_choice(attempts)
      elif choice==chosen_number:
        print("Congratulations! You made the right guess!")
      
      
        
    
    user_choice(attempts)