-2

A quick snippet of the code below. I tried messing around with another answer posted on here, but it didn't seem to work at all. I'm not sure what I'm doing wrong. Using Python 3 on Xubuntu 18.04 LTS. Here's the code:

while True:
    try:
        print("Your room is DARK, yet a light flashes red. What do you do?")
        print("")
        print("1. Look around.")
        print("2. There's a lamp somewhere...")
        print("3. Go back to bed.")
        print("")
        ans = int(input(">>> "))
        if ans == 1:
            print("")
            print("Too dark to see... better find a light...")
            time.sleep(2)
        if ans == 2:
            print("")
            print("Fumbling, you turn on your nightstand lamp...")
            break
        if ans == 3:
            print("")
            print("You sleep away the troubles... but you can't stay asleep...")
            time.sleep(1)
            print("")
            print("Back to the world of the living...")
        if ans == str('q'):
            sys.exit(0)
    except ValueError:
        print("")

So, when the user inputs "q", I want the program to close. I can't seem to get it to do it at all.

Prune
  • 76,765
  • 14
  • 60
  • 81
M. Knepper
  • 153
  • 2
  • 8
  • `ans = int(input(">>> "))` - this is trying to convert it to an integer. Therefore you'll never get `ans == str('q')` (also note `str('q') == 'q'`...) – jonrsharpe Jul 24 '18 at 15:56
  • Why convert to an integer at all? Leave it as a string and compare with "1", "2", "3" and "q". – Daniel Roseman Jul 24 '18 at 15:57
  • I have to admit I feel like a complete idiot. I don't know why I was converting everything to an integer. Having it as a string is much simpler. – M. Knepper Jul 24 '18 at 16:01

2 Answers2

1

The problem is with your line where you say int(input(">>> ")) which is converting what the user enters to an integer each time. What you should do is take in the user input as a string and then check if it is a valid number for 1, 2, & 3 or if it equals q.

Example:

ans = input(">>> ")
if ans == '1':
    # Do something
elif ans == '2':
    # Do something
elif ans == '3':
    # Do something
elif ans == 'q':
    sys.exit(0)
Karl
  • 1,664
  • 2
  • 12
  • 19
0

You're typecasting the q to an integer at input: ans = int(input(">>> ")) and then trying to typecast it back to a string at if ans == str('q'): Better solution would be to keep the input as a string in ans (remove the int() typecast and explicitly typecast it as an int with int() in each case.

Update: My original solution was wrong. The corrected one asks if the string is a digit and then evaluates it as an int. This is more verbose and I therefore recommend Karl's solution. But in case you're wedded to typecasting the string as an int I'm leaving this posted.

while True:
try:
    ans = input(">>> ")
    if ans.isdigit() and int(ans) == 1:
        ...
    elif ans.isdigit() and int(ans) == 2:
        ...
    elif ans.isdigit() and int(ans) == 3:
        ...
    elif ans == 'q':
        sys.exit(0)
except ValueError:
    print("")

Then you don't even need to call str() at all.

mas
  • 1,155
  • 1
  • 11
  • 28
  • This is the best solution, I feel. Thank you. All of you are right, though, it's silly to try and do it the way I was. I feel like a total moron. lol But thank you for your help! – M. Knepper Jul 24 '18 at 16:01
  • My solution is wrong. – mas Jul 24 '18 at 16:03
  • @M. I will delete this answer soon. But it is wrong. Entering q would cause an error because I'm typecasting a letter into a number. – mas Jul 24 '18 at 16:05
  • @M. I corrected it. But my recommendation is to stick with Karl's answer. – mas Jul 24 '18 at 16:10