Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Stop while loop after n iterations in python

I’m learning python and I am trying to create the guess game with two levels of difficulty: easy ( 10 tries) and difficult (5 tries). My code works well, but I need to force the while loop to stop asking for the guess after 5 tries in the difficult level and 10 tries in the easy level. However, with my code I did not reach my goal as the while loop does not stop after n variable < 5 or 10. How can I reach this goal this using the code below:

from random import *

def levels():
  user_level=input('Type E for easy level and D for difficult level: ')
  if user_level=='e':
    easy_level()
  else:
    difficult_level()

number=randint(1,100)

def difficult_level():
  n= 0
  while n < 5:
    user_number=int(input('Guess the number: '))
    if user_number > number:
      print('Too high')
    elif user_number < number:
      print('Too low')
    elif user_number == number:
      print(f'You guessed the number {number}! Congratulations!')
      play_again=input('Would you like to play again? type y for yes or n for no: ')
      if play_again =='y':
        levels()
      else:
        print('Bye')
        break
  print('Sorry, no more attempts :(')


def easy_level():
  n= 0
  while n < 10:
    user_number=int(input('Guess the number: '))
    if user_number > number:
      print('Too high')
    elif user_number < number:
      print('Too low')
    elif user_number == number:
      print(f'You guessed the number {number}! Congratulations!')
      play_again=input('Would you like to play again? type y for yes or n for no: ')
      if play_again =='y':
        levels()
      else:
        print('Bye')
         break
  print('Sorry, no more attempts :(')


levels()

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The short answer is that you need to add a n += 1 in the while loops, like that:

def easy_level():
  n= 0
  while n < 10:
    user_number=int(input('Guess the number: '))
    if user_number > number:
      print('Too high')
    elif user_number < number:
      print('Too low')
    elif user_number == number:
      print(f'You guessed the number {number}! Congratulations!')
      play_again=input('Would you like to play again? type y for yes or n for no: ')
      if play_again =='y':
        levels()
      else:
        print('Bye')
         break

    n += 1

  print('Sorry, no more attempts :(')

Long answer is that you should really consider using a for loop instead, here is an example of how you can do that:

def easy_level():
  for i in range(10)
    user_number=int(input('Guess the number: '))
    if user_number > number:
      print('Too high')
    elif user_number < number:
      print('Too low')
    elif user_number == number:
      print(f'You guessed the number {number}! Congratulations!')
      play_again=input('Would you like to play again? type y for yes or n for no: ')
      if play_again =='y':
        levels()
      else:
        print('Bye')
         break

  print('Sorry, no more attempts :(')

And you should make your script a lot cleaner by removing the repetitive code like this:

from random import *

def choseLevel():
  user_level=input('Type E for easy level and D for difficult level: ')
  if user_level=='e':
    return 10
  else:
    return 5

number = randint(1,100)
for i in range(choseLevel):
    user_number = int(input('Guess the number: '))
    if user_number > number:
      print('Too high')
    elif user_number < number:
      print('Too low')
    elif user_number == number:
      print(f'You guessed the number {number}! Congratulations!')
      play_again = input('Would you like to play again? type y for yes or n for no: ')
      if play_again =='y':
        levels()
      else:
        print('Bye')
         break

  print('Sorry, no more attempts :(')

Here I removed the two functions that you had and I made it so that there is only one loop which makes the code a lot cleaner, I also changed the levels() function name to choseLevel() to make it clearer on what the function does and I also added spaces between the = which makes things look cleaner, Thanks.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading