0

So I'm trying out the Bagels Pico Fermi game, but I'm trying to write the code without looking to learn.

https://inventwithpython.com/bigbookpython/project1.html

I've written and rewritten this code at least 10 times, and as far as I can tell, the logic makes sense. However, whenever running the code and guessing, I either get all "Bagels" or one Fermi and 2 Bagels.

I am VERY new to python, so I am not really sure what to look up to fix this without just directly copying the working code from the sample.

print('Welcome to Bagels! The Deductive Logic Game!')

print('''The rules are simple,
Pico: The digit is correct but in the wrong position.
Fermi: The digit is correct and in the right position.
Bagels: The digit is not correct.
''')

print('You have 10 guesses to guess the secret 3 digit number, what is your first guess?')

import random

secretint = random.randint(1, 999)
secret = str(secretint).zfill(3)
print(secret)

guesses = 0
while guesses <= 10:
    guess = str(input())
    # guess is equal to secret
    if guess == secret:
        print('VICTORY')
        exit()
    else:

        guesses += 1
        digit1 = secret[:1]
        digit2 = secret[:2]
        digit3 = secret[:3]
        guess1 = guess[:1]
        guess2 = guess[:2]
        guess3 = guess[:3]

        if guess1 != digit1 and (guess1 == (digit2 or digit3)):
            print('Pico')
        else:
            if guess1 == digit1:
                print('Fermi')
            else:
                print('Bagels')

        if guess2 != digit2 and (guess2 == (digit1 or digit3)):
            print('Pico')
        else:
            if guess2 == digit2:
                print('Fermi')
            else:
                print('Bagels')

        if guess3 != digit3 and (guess3 == (digit1 or digit2)):
            print('Pico')
        else:
            if guess3 == digit3:
                print('Fermi')
            else:
                print('Bagels')

if guesses <= 11:
    print('Out of guesses, goodbye!')
  • `guess1 == (digit2 or digit2)` does not do what you think it does. – mkrieger1 Oct 22 '21 at 14:28
  • `digit2 = digit[:2]` and `digit3 = digit[:3]` also does not do what you think it does. – mkrieger1 Oct 22 '21 at 14:32
  • You should read the great articles [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [Find a simpler problem](https://ericlippert.com/2014/03/21/find-a-simpler-problem/). – mkrieger1 Oct 22 '21 at 14:34
  • As well as [How to step through Python code to help debug issues?](https://stackoverflow.com/questions/4929251/how-to-step-through-python-code-to-help-debug-issues) – mkrieger1 Oct 22 '21 at 14:45
  • I will look into this thank you. – Kyle Kubiak Oct 22 '21 at 15:04

0 Answers0