How can i matched the random generated number in input function?
my if and else statement don’t run because even i inputted the random number, they don’t match. here is my code below.
import random
randomNumber=[]
length_randomNumber = 6
for i in range(length_randomNumber):
randomNumber.append(random.randint(0,9))
print(randomNumber)
x = input("Enter the 6 generated number :")
if x == randomNumber:
print("x and random number matched!")
else:
print("x don't matched with the random number!")
>Solution :
Here the problem occurs due to x = input("Enter the 6 generated number :").
The x here is of string type.
What you can do instead is this:
for i in range(length_randomNumber):
randomNumber.append(random.randint(0,9))
print(randomNumber)
x = input("Enter the 6 generated number, seperated by commas :")
x = [int(y) for y in x.split(",")]
if x == randomNumber:
print("x and random number matched!")
else:
print("x don't matched with the random number!")
Here you convert the input to an integer array and compare.
Above we used a list comprehension to do that.