0

I was trying out python and i made up this game kind of thing. The if condition doesnt seem to be working as i want it to.Please educate me if there is a misconception in the semantics or anything. PS : I ran this on python 3

import time
from random import randint

def tambola():

    token = input("Enter a value between 1 to 2 :  ")
    print('')
    print("Computer generating number",end='')
    time.sleep(1)
    print(".",end='')
    time.sleep(1)
    print(".")
    time.sleep(1)

    answer = randint(1,2)
    if(token == answer):  #condition doesnt seem to be working :/
        print("You win")
    else:
        print("Better luck next time")
        print(token,answer)

This is the output i am getting

Enter a value between 1 to 2 :  2

Computer generating number..
Better luck next time
2 2
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 3
    Simple, token = input("Enter a value between 1 to 2 : "), the token you got is a str, not int, you have to convert/cast it to int. Has nothing to do with the if statement. – Menglong Li Feb 23 '18 at 03:55
  • omg it works !Thanks a lot dude. Ill remember this – Tnv Madhav Feb 23 '18 at 03:57
  • What you get from input is a “string” but the answer you get is a “int”, I guess this is the reason. I can’t type in too many since I’m answering from iPhone. – xudesheng Feb 23 '18 at 03:57
  • Could you explain why you think it should work? Python might not force you to define types, but they're still there – OneCricketeer Feb 23 '18 at 03:58
  • I learnt it now thanks to Menglong Li and Windchill, i have worked out that typecast was missing – Tnv Madhav Feb 23 '18 at 03:59
  • Lots of other similar post's about if statements not working... Might want to search next time? https://stackoverflow.com/questions/30593331/if-condition-not-working-properly-inside-a-function-python-3 – OneCricketeer Feb 23 '18 at 04:01
  • Thanks for the help – Tnv Madhav Feb 23 '18 at 04:03

0 Answers0