-1

I originally wrote this program in python 2, and it worked fine, then I switched over to python 3, and the while loop working.

I don't get any errors when I run the program, but it isnt checking for what the value of i is before or during the run. The while loop and the first if loop will run no matter what.

#imports the random module
import random

#Creates variable that is used later
i = 0

#chooses a random number betweeen 1 - 100
randomNumber = random.randint(1,10)

#prints the number
print (randomNumber)

#Creates while loop that runs the program until number is guessed
while i == 0:

    #Creates a variable where the answer will be stored, and then asked the question in the quotes
    user_answer = input("Try to guess the magic number. (1 - 10) ")

    print ("\n")

    if user_answer == randomNumber:
        print("You guessed correct")
        break
    else:
        print("Incorrect. Try again.")

Thanks for any help in advance.

10
  • 1
    What do you mean by "First if loop"?
    – Davy M
    Jan 5, 2018 at 1:30
  • 2
    input returns a string, not an integer. Jan 5, 2018 at 1:31
  • 1
    input has been updated in python3 to no longer try to treat the input as a python expression, meaning that while user_answer in python2 would be an integer if they input something that looks like an integer, it will in python3 always be a string. That just means you'll need to cast it to an integer before comparing it to randomNumber
    – Hamms
    Jan 5, 2018 at 1:31
  • Also, why are you checking on i and then breaking? Why not incorporate your condition into the loop .
    – Pythonista
    Jan 5, 2018 at 1:32
  • I forgot to change the break to i = 1
    – Kirby127
    Jan 5, 2018 at 1:32

2 Answers 2

5

You are comparing something like '6' == 6, since you didn't convert the user input to an int.

Replace user_answer = input("Try to guess the magic number. (1 - 10) ") with user_answer = int(input("Try to guess the magic number. (1 - 10) ")).

1
  • that fixed the problem. Thanks
    – Kirby127
    Jan 5, 2018 at 1:36
0

user_answer will store the input as string and random.randint(1,10) will return an integer. An integer will never be equal to a string. So you need to convert user_input to integer before checking.

#imports the random module
import random

#Creates variable that is used later
i = 0

#chooses a random number betweeen 1 - 100
randomNumber = random.randint(1,10)

#prints the number
print (randomNumber)

#Creates while loop that runs the program until number is guessed
while i == 0:

    #Creates a variable where the answer will be stored, and then 
    asked the question in the quotes
    user_answer = input("Try to guess the magic number. (1 - 10) ")

    # better use exception handling here
    try:
        user_answer = int(user_answer)
    except:
        pass

    print ("\n")

    if user_answer == randomNumber:
        print("You guessed correct")
        break
    else:
        print("Incorrect. Try again.")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.