-1

I'm trying to make something where you you are given a random number, and you are supposed to input that number. I don't know how to make python check to see if the input is the same as a variable. The current code doesn't recognize it as the variable. I've looked around and everything doesn't yield any results related to this. I'm pretty new to python, so it might just be super obvious.

import random
rand = random.randint(1000,9999)
print(rand)
question=input("What is the number?")

if question==rand:
    print("That is the number.")

else:
    print("That is not the number.")

2 Answers2

1
random_number = 1234 # Set this to your random number
if int(input(f"Please input {random_number}: ")) == random_number:
    pass # Replace this statement with what you want to do if the user inputs the correct number

Use the input() function provided in the standard library to get input from the user in the console. The parameter passed (a string) will be printed out and when you type in the console, it will show up next to it. Press enter to send it to Python.

EDIT: Forgot the "f" before the string.

Use the == operator to compare two things. It returns true if they have the same value.

Since input() returns a string, and you are comparing it against a number, you must turn the string from the input into a number. int() does this. If you want decimals, use float().

Unsigned_Arduino
  • 357
  • 2
  • 16
1
import random
rand = random.randint(1000,9999)
print(rand)
num=input("What is the number: ")

if int(num)==rand:
    print("That is the number")

else:
    print("That is not the number")

The num is converted to integer as by default the input that we take from user is in string format.