Python - 如何让它读取代码?

时间:2016-03-12 11:45:23

标签: python python-3.x

这是我的代码

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!")

当它询问问题时,以及' y'它只是直接进入"呜呜!"。我想要它做的是打印"它的工作原理"但是,如果你输入n,它只是去呜呜。请帮忙

2 个答案:

答案 0 :(得分:3)

这应该有效:

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

if ans1.lower() == 'y':
    print("It works")
else:
    print("Woo Hoo!")

答案 1 :(得分:0)

我建议使用distutil strtobool来涵盖所有案例

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!")