Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to matched random number using input function

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading