abdulqgg
abdulqgg

Reputation: 115

i dont understand why my "if" command don't work as it should

import random  
cde = random.random()  
print(cde)  
tst = input("Whats the code?\n")  
if tst == cde:  
    print("Welcome")  
else:  
    print("Imposter!!!")

when i run this code and after I've typed in the "cde" thing its keeps saying imposter but it should say welcome

Upvotes: 0

Views: 103

Answers (5)

Adaephon
Adaephon

Reputation: 18329

The issue depends on your version of Python:

  • For Python 2.x the issue is that

    print(cde)
    

    prints only the first 12 decimal digits of cde. So if you just type that for tst it will in most cases not match as there are several more digits, you just do not see. You can test this in an interactive Python session:

    >>> import random
    >>> a = random.random()
    >>> a
    0.10981364144091466
    >>> print a
    0.109813641441
    
  • For Python 3.x the issue is that with

    cde = random.random()
    

    cde is of type builtins.float, while with

    tst = input("Whats the code?\n") 
    

    tst is of type builtins.str. So comparing these two will always return False.

An additional issue is that comparing floats for equality (==) is not always reliable. The printed decimal value does not exactly match the internal binary value.

I would suggest generating a random integer and casting the input also to integer:

import random  
cde = int(random.random() * 1000000)
print(cde)  
tst = int(input("Whats the code?\n"))
if tst == cde:  
    print("Welcome")  
else:  
    print("Imposter!!!")

This code work with Python 2.x and 3.x, but for Python 2.x it would be a good idea to use raw_input() instead of input(). You could just use str() instead of int(), but using integers instead of casting to str has the benefit that you also could easily make numerical lesser/greater comparisons.

Upvotes: 0

hiro protagonist
hiro protagonist

Reputation: 46849

random returns a float that you are comparing with a string from the user input - they will never be equal...

you could try:

import random  
cde = str(round(random.random(), 3))
print(cde)  
tst = input("Whats the code?\n")        # <- python3
# tst = raw_input("Whats the code?\n")  # <- python2
if tst == cde:  
    print("Welcome")  
else:  
    print("Imposter!!!")

and in order to avoid rounding effects you may consider rounding the result from random.random().

note the different ways of getting user input depending on the python version you are using.

Upvotes: 3

Francisco Vargas
Francisco Vargas

Reputation: 652

It is because you are not casting so in this case you are comparing a string to a long. Simple fix (python 2 only):

import random  
cde = random.random()  
print(cde)  
tst = input("Whats the code?\n")  
if long(tst) == long(cde):  
    print("Welcome")  
else:  
    print("Imposter!!!")

Upvotes: 0

user4398691
user4398691

Reputation:

Maybe it will work this way: str(tst) == str(cde)

Upvotes: 0

Beri
Beri

Reputation: 11600

Random will return an float, whereas you are reading from STDIN an string, you need to cast them to a common type:

import random  
cde = random.random()  
print(cde)  
tst = input("Whats the code?\n")  
if str(tst) == str(cde):  // cast to a common type, string or a long, float
    print("Welcome")  
else:  
    print("Imposter!!!")

Upvotes: 1

Related Questions