0

I am trying to make a quest in python terminal but when i try to compare two code it says its wrong

import time
import random

def adventure():
   
    key = random.randint(111,999)

    delay = 1

    print()
    print("the code is", key)
    
    time.sleep(delay)

    uk = input("do you remember the code?")

    if uk == key:
        print("Good")

    else:
        print("wrong code")

Edit: so the solution was instead of

if uk == key:

do this

if int(uk) == key:
  • It is probably type issue, so you need to change the variable uk into int or change key to str before comparing them. Also, try printing out the values next time. It will help you find bugs in your code. – noninertialframe Sep 09 '20 at 07:45
  • Does this answer your question? ["is" operator behaves unexpectedly with integers](https://stackoverflow.com/questions/306313/is-operator-behaves-unexpectedly-with-integers) – Peter O. Sep 09 '20 at 08:01

1 Answers1

1

A good explanation can be found here

Essentially don't use 'is' to compare integers. Change your code at this point here and replace 'is' with '==' and it will work

if uk == key:
    print("Good")