0
import random
randomNum=random.randint(1,9999)
print(randomNum)
userGuess=input("Please guess a 4 digit number, if you get it correct, we'll tell you, otherwise we will tell if you got any of the positions of the numbers correct\n")
if userGuess==randomNum:
    print("Congratulations, you guessed the number correctly")

Why doesn't last line work after i enter the correct number? I don't think its indentation and I'm confused.

  • 2
    Does this answer your question? [How can I read inputs as numbers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers) – Patrick Haugh Jan 07 '20 at 18:17

2 Answers2

2

You need to cast userGuess as an integer. i.e.

import random
randomNum=random.randint(1,9999)
print(randomNum)
userGuess=int(input("Please guess a 4 digit number, if you get it correct, we'll tell you, otherwise we will tell if you got any of the positions of the numbers correct\n"))
if userGuess==randomNum:
    print("Congratulations, you guessed the number correctly")
Teymour
  • 1,832
  • 1
  • 13
  • 34
1

By default input is considered as str and not the data type of entered value, that's why you need to take the input and covert it to int or convert the randomNum to str.

import random
randomNum= str(random.randint(1,9999))
print(randomNum)
userGuess=input("Please guess a 4 digit number, if you get it correct, we'll tell you, otherwise we will tell if you got any of the positions of the numbers correct\n")
if userGuess==randomNum:
    print("Congratulations, you guessed the number correctly")

OR

import random
randomNum=random.randint(1,9999)
print(randomNum)
userGuess=int(input("Please guess a 4 digit number, if you get it correct, we'll tell you, otherwise we will tell if you got any of the positions of the numbers correct\n"))
if userGuess==randomNum:
    print("Congratulations, you guessed the number correctly")