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


import random   cde = random.random()   print(cde)   tst = input("whats code?\n")   if tst == cde:       print("welcome")   else:       print("imposter!!!") 

when run code , after i've typed in "cde" thing keeps saying imposter should welcome

random returns float comparing string user input - never equal...

you try:

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

and in order avoid rounding effects may consider rounding result random.random().

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


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -