1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] random variable match/case [duplicate]

Discussão em 'Python' iniciado por Stack, Setembro 17, 2025.

  1. Stack

    Stack Membro Participativo

    I tried to make a secret number game, but with 4 correct numbers using match/case statement like this:

    import random
    secret_num = random.randint(1,20)
    secret_num2 = random.randint(1,20)
    secret_num3 = random.randint(1,20)
    secret_num4 = random.randint(1,20)
    num = int(input("Enter a number between 1 and 20 : "))
    match num:
    case secret_num | secret_num2 | secret_num3 | secret_num4:
    print("You won!")
    case _:
    print("You lost!")


    but it didn't work, I am a beginner and GPT-5's explanation was weird, he said that Python thinks that you are making a new variable in the match/case statement or something. I will be very thankful to anyone who answers in a simple way.

    I wanted this to happen :

    Enter a number between 1 and 20 : 5
    You won!
    Enter a number between 1 and 20 : 7
    You won!
    Enter a number between 1 and 20 : 8
    You won!
    Enter a number between 1 and 20 : 17
    You won!


    4 correct numbers, and the rest are wrong.

    Continue reading...

Compartilhe esta Página