0

Here is my code

import time
print("------------------------------------------")
print("  Welcome to Senpai Quest")
print('------------------------------------------')
time.sleep(3)
print("")

print('You have English next, there is a test. If you hit yes .you go to     
class and you get +1 charisma and Knowledge.')
print("")
print('If you hit no, you will skip class and get +1 Courage and +1 risk.')
ans1=str(input('Do you Take the english test? [Y/N]'))

if ans1== ['y']:
    print("It works")

else:
    print("Woo Hoo!")

When it asks the question and for the 'y' it just goes straight through to "woo hoo!". What i would like it to do is to print "It works" but if you type n it just goes to woo hoo. Please help

  • 1
    You are checking your string against a list with a string in it. Voting to close for simple typographical error. – timgeb Mar 12 '16 at 11:46

2 Answers2

3

This should work:

ans1 = input('Do you Take the english test? [Y/N]').strip()

if ans1.lower() == 'y':
    print("It works")
else:
    print("Woo Hoo!")
danidee
  • 9,298
  • 2
  • 35
  • 55
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • This will work for python 3.x, however, if you're using 2.x you should probably consider using `raw_input` because `input` evaluates your input, can read more about it [here](http://stackoverflow.com/questions/4915361/whats-the-difference-between-raw-input-and-input-in-python3-x) – Deusdeorum Mar 12 '16 at 12:07
  • @HugoHonorem question is tagged as `python-3.x` – Óscar López Mar 12 '16 at 12:19
  • Óscar-lópez: true, sloppy reading of the tags – Deusdeorum Mar 12 '16 at 12:29
0

I suggest using the distutil's strtobool in order to cover all cases

from distutils.util import strtobool

ans1=input('Do you Take the english test? [Y/N] ').strip()

if strtobool(ans1):
    print("It works")

else:
    print("Woo Hoo!")
Bharel
  • 23,672
  • 5
  • 40
  • 80