I am attempting at making a text based game that uses dictionaries as "case switch" blocks (which I learned from this website). I am not on that part. However, I am wondering why this code below will not execute game_main()
in the if
statement.
That narrows down to the problem being ans == 1
, but when entering 1
and printing it out before the comparison, it skips the if
and goes to the else
statement every time.
So, I'm thinking the problem is that I don't know what variable type the variable ans
is, and that I need to cast it to a certain type. Or, I need to force a certain type of input to be used by the user... Or you all are a ton smarter than I am and probably have an awesome way of fixing this!
I'm new to python and really only know java.
print ("Welcome to _________!")
ans = input("1. Start\n2. How to play\n3. Not now (exit)\n\n")
print ("\nYou have entered " + ans)
try:
if (ans == 1 or ans == "1"):
game_main()
elif (ans == 2):
print (dict_startmenu[2])
elif (ans == 3):
print (dict_startmenu[3])
else:
print("*****else block")
except Exception as e:
print("fatal error in start block")