在codecademy中,我运行了一个简单的python程序:
choice = raw_input('Enjoying the course? (y/n)')
while choice != 'y' or choice != 'Y' or choice != 'N' or choice != 'n': # Fill in the condition (before the colon)
choice = raw_input("Sorry, I didn't catch that. Enter again: ")
我在控制台输入y,但循环从未退出
所以我换了一种方式
choice = raw_input('Enjoying the course? (y/n)')
while True: # Fill in the condition (before the colon)
if choice == 'y' or choice == 'Y' or choice == 'N' or choice == 'n':
break
choice = raw_input("Sorry, I didn't catch that. Enter again: ")
这似乎很管用。不知道为什么
你的逻辑颠倒了。使用
and
代替:通过使用
or
,输入Y
意味着choice != 'y'
是真的,所以其他or
选项不再重要。or
意味着选项中的一个必须为true,并且对于任何给定的choice
值,始终至少有一个!=
测试将为true。您可以通过使用
choice.lower()
来节省一些输入工作,只对y
和n
进行测试,然后使用成员资格测试:相关问题 更多 >
编程相关推荐