Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this code not working (i'm new to programming, python)

import random

guesses = 3
number = random.randint(1, 10)
print (number) #<- To test what number im supposed to write.

while guesses > 0: # When you have more than 0 guesses -> guess another number
    guess = input("Guess: ")

    if guess == number : # If guess is equal to number -> end game
        print ('Your guess is right!.')
        break


    if guess != number : # If not number -> -1 guess
        print ("Nope!")
        guesses -= 1

    if guesses == 0: #If all 3 guesses are made -> end game
        print("That was it! Sorry.")
        print(number,"was the right answer!")

What am i doing wrong? I can't figure it out, i hope you can help ^-^

And if you are able to teach me how to improve my programming then feel free to write me how to do it! Im open for learning new stuff btw sorry for my bad english :3 (edit: When i guess the right number, it still says "Nope!" and i have to guess another number.)

like image 369
Venx Avatar asked Apr 27 '26 20:04

Venx


1 Answers

This looks like Python3. If so, use guess = int(input("Guess: ")) instead.

In Python3 input() returns a string and you're comparing that string to an integer which will never work. So convert the return value of input() to an integer to be sure you're comparing apples to apples.

like image 57
mechanical_meat Avatar answered Apr 30 '26 10:04

mechanical_meat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!