[Python] Compare string variables to another string variables

Discussão em 'Python' iniciado por Stack, Abril 25, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to compare digits of a string to other digits of another string. guess_str is my input turned into a string and then num_str is the randomly generated number turned into a string.

    Example:

    guess_str: 100

    num_str: 201

    How can I have it compare the two strings so that when I type 100 as guess_str it will compare to num_str showing that the number "1" that was input is part of num_str but not in the same spot?

    In this I am wondering only about the second elif statement.

    num = random.randint(100,999)
    attempts = 1
    max_attempts = 10

    print(main())
    print(num)

    while attempts <= max_attempts:
    guess = int(input(f"Guess#{attempts}:\n> "))
    if guess == num:
    print("Hooray!! That is the correct number!")


    answer = input("Enter yes or no: ")
    if answer.lower() == "yes":
    main()
    elif answer.lower() == "no":
    print("Ok have a great day!")
    break

    else:
    guess_str = str(guess)
    num_str = str(num)

    for i, val in enumerate(str(num)):
    if guess_str == val:
    print("Fermi".format(i))

    elif any(x in num_str for x in guess_str):
    print("Pico")

    elif guess_str != num_str:
    print("Bagels")
    break

    #adds attempt to game
    else:
    attempts += 1

    Continue reading...

Compartilhe esta Página