-2

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")
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
chuck54
  • 1
  • 2
  • 1
    Works fine for me. Can you make sure your're giving correct input. What is the python version you're using? – pramesh May 03 '18 at 04:00
  • I removed the offensive language you were using, which wasn't appropriate for this site. However, I had to guess at the meaning which you were attempting to convey. Please feel free to correct it to something equivalent to what you desired, but without the offensive wording. Thanks. – Makyen May 03 '18 at 04:02
  • 1
    `input` returns `string` type and you try to compare with `int` type which allways would be `False` ( except the first if because of comparsion with string ) – Yaroslav Surzhikov May 03 '18 at 04:10
  • 1
    Possible duplicate of [How can I read inputs as integers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-integers) – tripleee May 03 '18 at 04:18
  • By the way, it's almost always a bad idea to do `except Exception as e:` and then not print out the exception. That makes it impossible to debug which of the many possible exceptions you've gotten, because they all just print the same `fatal error in start block` message. – abarnert May 03 '18 at 04:48

1 Answers1

0

You should define integer when input 1,2 or 3

ans = int(input("1. Start\n2. How to play\n3. Not now (exit)\n\n "))

print out :

print ("\nYou have entered "), ans

then:

if (ans == 1):
Thanh Nguyen Van
  • 10,292
  • 6
  • 35
  • 53
  • 1
    You will get ValueError if non-numeric value is prompted. Before converting to int - you should validate input. Don't trust user: trust can be expensive for you and may have unforeseen consequences – Yaroslav Surzhikov May 03 '18 at 04:45
  • @YaroslavSurzhikov The rules for what strings are and aren't valid integers are very complicated, and change from version to version. If you want to know whether something can be safely converted to `int`, just `try:` it and `except ValueError:`. – abarnert May 03 '18 at 04:46
  • @abarnert `.isdigit()` work's same on 2.7, 3.4 and 3.6 . It's not so complicated. And in any case i've posted my comment just for pointing to possible vulnerability of passing through input from user without data validation – Yaroslav Surzhikov May 03 '18 at 04:58
  • @YaroslavSurzhikov No, it only works the same across versions on ASCII digits. And that's not the same rule used by `int` in _either_ version. And of course it's obviously wrong for things like, say, `'-2'`. More importantly: _what_ vulnerability. `int` will either return an int, or raise a `ValueError`. The Pythonic way to handle that is `except ValueError:`. – abarnert May 03 '18 at 04:59
  • Thank you all for the help! I apologize for the edits that needed to be made, totally forgot to sensor my little names for things... Also I have never understood the best way to use exceptions, and this is why I love this website; you all help with things I would have planned to ask next! – chuck54 May 03 '18 at 19:54