Help in Python! Guessing game!

Asked

Viewed 715 times

1

I need some help in this little game. The point is this, the game I’ve made and it’s working perfectly, what I need actually is the loop with the While.

I can not find the error, while the user does not hit the value I need the game to continue until the fifth attempt, however mine is not running the while and I can’t understand why.

Follow the code below:

from random import randint


def acertou():

n = randint(1,11)

x = 0

print(n)


while (x < 5):

    x = x + 1
    y = int(input("Digite um número "))
    if n == y:
        return "Parabéns,você acertou!"
        break
    elif y > n:
        return "Opa,o número que você escolheu é maior que o sorteado!"
    return "Opa,o número que você escolheu é menor que o sorteado!"
  • 3

    Tip: arrange the indentation of your code. In Python it is fundamental and needs to be correct. Who knows the code works? Another tip: you have a function (acertou()) who will need to be called by someone.

1 answer

1


Apparently, you want your function to return only if it hits, or misses more than 5 times, but in the final two lines:

elif y > n:
    return "Opa,o número que você escolheu é maior que o sorteado!"
return "Opa,o número que você escolheu é menor que o sorteado!"

Voce is returning, leaving the function returning the text. while does nothing, because no matter which way, the code returns. ( break after Return also makes no sense, Return has already left the code of the entire function )

switch to a print and you will finish the function only if you hit, or miss more than 5 times.

Obs: assuming you are receiving data from the console.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.