Home > Mobile >  How to stop a while loop after n iterations?
How to stop a while loop after n iterations?

Time:05-14

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

CodePudding user response:

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 chooseLevel():
  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(chooseLevel()):
    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 chooseLevel() to make it clearer on what the function does and I also added spaces between the = which makes things look cleaner.

I also used the for loop like this for i in range(chooseLevel()) Which means that if the chooseLevel() function returned a 5 it will be as if I wrote for i in range(5) and if the chooseLevel() function returns a 10 it will be as if I wrote for i in range(10)

Thanks.

CodePudding user response:

Looks like you forgot to increment n in your loops, so n is always = 0. Usually you increment your "tracker" variable at the start of the loop or the end of the loop. Incrementing in python can be done with n = 1.

CodePudding user response:

You need to increment n after each iteration, by either reassigning n (ex. n = n 1), or using the = operator like this:

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

    n  = 1 # increment n after each iteration

  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

    n  = 1 # increment n after each iteration

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


levels()

CodePudding user response:

Probably easier to use a for loop like so:

def levels():
  easy_turns = 5
  hard_turns = 10
  user_level=input('Type E for easy level and D for difficult level: ')
  if user_level=='e':
    play(easy_turns)
  else:
    play(hard_turns)

def play(turns):
  for _ in range(turns):
    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 :(')

A good rule of thumb is if you are using the same code twice put it in a function. It's sure to get out of sync over time then you'll be wondering why "easy" works but "hard" does something odd (or some variation of that).

  • Related