Infinite While Loop while making a simple logic game "Bagels"

Question:

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')
Asked By: Jos

||

Answers:

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
Answered By: The Thonnu
  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."

Answered By: Svenito
import random

choice_of_nums = ['123', '345', '674', '887', '356', '487', '916']
random_three_num = random.choice(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! ')
    while not guess.isdigit():
        guess = input('You must choose integer values! ')

    number_is_present = any(number in guess for number in random_three_num)

    if guess == random_three_num:
        print('YOU WIN! Well done')
        chances = 1  # combined w/ the last line, chances will become = 0
    elif not number_is_present:
        print('Bagel')
    else:
        index_is_right = False
        for i in range(len(guess)):
            if guess[i] == random_three_num[i]:
                index_is_right = True
        if index_is_right:
            print('Fermi')
        else:
            print('Pico')
    chances -= 1
  • (06/28/22) added chances = 1 if the guess is right, so to exit the while loop

  • random.choices returns a list

  • you don’t need the re module

  • use snake case as suggested in PEP8

Answered By: Jonathan Ciapetti
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.