简体   繁体   中英

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.)

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.

You need to put int in front of input, so:

guess = int(input("Guess: "))

This will turn guess into an integer, so the code recognises it.

The input() command returns a string and a string is not equal to a number ( "3" == 3 evaluates to false ). You can use the int(...) function to turn a string (or floating-point number) into an integer.

I'm assuming you're using Python 3.x given that print is a function. If you were using Python 2.x, you should use raw_input() instead, as input() causes the interpreter to treat what was entered as Python code and execute it (like the eval(...) function does).

In 99.999 % of all cases, you do not want to execute user input. ;-)

Another rather major thing your program needs is to prompt the user so they know what they will be doing with your program exactly. I've added the prompt accordingly.

import random

print ("Hello. We are going to be playing a guessing game where you guess a random number from 1-10. You get three guesses.")
number = random.randint(1, 10)
# print (number) #<- To test what number im supposed to write.
guesses = 3
while guesses > 0: # When you have more than 0 guesses -> guess another number
    guess = input("Enter your 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!")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM