[Python] Pico Fermi Bagel: Why incorrect case printed when 0 in list of numbers?

Discussão em 'Python' iniciado por Stack, Maio 2, 2025.

  1. Stack

    Stack Membro Participativo

    Disclaimer: Current code may have more bugs than I see at the moment.

    Code


    import random

    def explain_instructions():
    print("I am thinking of a number with nonrepeating digits. You will have 10 attempts to try and guess my number.")
    print("'Bagel' will be displayed if no digits are correct.")
    print("'Pico' will be displayed if a digit is correct but in the wrong place.")
    print("'Fermi' will be displayed if a correct digit is in the correct place.")

    def generate_number(length):
    num_list = [0,1,2,3,4,5,6,7,8,9]
    random.shuffle(num_list)
    secret_num = num_list[0:length]
    secret_num = "".join(str(digit) for digit in secret_num)
    return secret_num

    def give_clues(secret_num, guess):
    clues = []
    for i in range(len(str(guess))):
    if guess == secret_num:
    clues.append("Fermi")
    elif guess in secret_num and guess != secret_num:
    clues.append("Pico")
    if clues == []:
    clues.append("Bagel")
    return(clues)
    print(clues)


    def get_guess(length, guess):
    for i in range(int(length)):
    if guess in guess[:i] or guess in guess[i+1:]:
    print("Repeating numbers don't work in this game.")
    return
    elif len(guess) != len(secret_num):
    print("You don't have the correct number of digits.")
    return
    elif guess not in num_list and guess != "":
    print(i,"You can only guess numbers.")
    return
    else:
    return int(guess)

    def play_again():
    print("Would you like to play again? (Yes/No)")
    answer = input()
    if answer.lower()== "yes":
    return True
    else:
    print("That wasn't a firm 'yes' so.... goodbye :( ")


    print("Welcome to Bagel, Fermi, Pico!")
    explain_instructions()
    num_list = [0,1,2,3,4,5,6,7,8,9]
    game_is_done = False

    while True:
    print("How long would you like your number to be?")
    length = input()
    secret_num = generate_number(int(length))
    print(secret_num)

    max_guess = 0
    while max_guess < 10:
    print("Enter a", length, "digit guess:")
    guess = input()
    if guess == "411":
    print(explain_instructions())
    elif get_guess(length,guess):
    max_guess += 1
    clue = give_clues(secret_num,guess)
    print(clue)

    if clue == ['Fermi'] * len(secret_num):
    print("Congrats! You guessed the correct number!")
    break
    if max_guess == 10:
    print("Oh no! You have run out of guesses. The secret number was:", secret_num)


    if not play_again():
    break

    Issue


    What I am trying to fix is the get_guess() function.

    At the moment I have coded it to print i in the for i in range because whenever I input a guess, it automatically assumes i = 0 and prints

    You can only guess numbers.


    I'm not sure why, when 0 is part of the list of numbers that it is supposed to check.

    Any ideas on how to fix this?

    Continue reading...

Compartilhe esta Página