Home > Enterprise >  Infinite While Loop while making a simple logic game "Bagels"
Infinite While Loop while making a simple logic game "Bagels"

Time:06-16

I am using The Big Book of Small Python projects to increase my skills in python, and on the very first project, which is making a simple logic game, On the first try, the code goes all the way however if you get it wrong you it won't run properly.

Here is the code and a description of the game, the while loop with chances is supposed to run for the whole game, until you run out of chances, the second while loop is supposed to run in case user enters below or more than length three for the number

import re
import random

#In Bagels, a deductive logic game, you
#must guess a secret three-digit number
#based on clues. The game offers one of
#the following hints in response to your guess:
#“Pico” when your guess has a correct digit in the
#wrong place, “Fermi” when your guess has a correct
#digit in the correct place, and “Bagels” if your guess
#has no correct digits. You have 10 tries to guess the
#secret number.



choice_of_nums=['123','345','674','887','356','487','916']
random_three_num=random.choices(choice_of_nums)
count_bagel=0
count_fermi=0
Chances=10

while Chances!=0:
 guess = input(f'Guess the three digit number! You have {Chances} to guess! ')
 while len(guess)!=3:
    guess=input('You must choose a three digit number! Try again! ')

 for i in range(0,len(random_three_num)):
    if guess==random_three_num:
        print('YOU WIN! Well done')
        break
    elif guess[i] not in random_three_num:
        count_bagel =1
        if count_bagel==len(random_three_num):
            print('Bagels')
            Chances=Chances-1
    elif guess[i]==random_three_num[i]:
        count_fermi =1
        Chances=Chances-1
        print('Fermi')
    elif guess in random_three_num:
        print('Pico')

CodePudding user response:

The break after print('YOU WIN! Well done') exits the for loop not the while loop. Put Chances = 0 before the break:

    if guess==random_three_num:
        print('YOU WIN! Well done')
        Chances = 0
        break

CodePudding user response:

  1. You should never check a while loop with a condition like x != 0. Always use <= or >=. The reason being, that if somehow the number zero is skipped and you end up at -1 then the loop will still exit.

  2. Couldn't your check if guess==random_three_num: be done before the for loop? Then the break statement would actually break the while loop. Now it's only breaking the for loop. This is one reason that could lead to a infinite loop.

  3. Your second to last line elif guess in random_three_num: should probably be elif guess[1] in random_three_num:.

  4. Chances=Chances-1 could probably be outside the for loop also, as the number of chances should decreasing only one per guess. Currently the number of chances decreases up to 3 times during the for loop (every time you hit 'Fermi'). This could lead to issue described in "1."

  • Related