Python - if语句无法正常工作 [英] Python - if statement not working correctly

查看:121
本文介绍了Python - if语句无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用python并且已经陷入了一些在我看来显然应该工作的东西。这是我的第一个代码,我只是尝试与用户进行对话。

I've just started using python and have got stuck on something that, in my mind, clearly should work. This is my first code and I just try to have a conversation with the user.

year = input("What year are you in school? ")
yearlikedislike = input("Do you like it at school? ")
if (yearlikedislike == "yes" or "Yes" or "YES" or "yep" or "yup" or "Yep" or "Yup"):
    print("What's so good about year " + year, "? ")
    input("")     
    print("That's good!")
    time.sleep(1)
    endinput = input("I have to go now. See you later! ")
    exit()
if (yearlikedislike == "no" or "No" or "nope" or "Nope" or "NOPE"):
    print("What's so bad about year " + year, "?")
    input("")
    time.sleep(1)
    print("Well that's not very good at all")
    time.sleep(1)
    endinput = input("I have to go now. See you later! ")
    time.sleep(1)
    exit()

我的问题是,即使我回答否定回答,它仍然会回复回复,就像回复一样我已经说过是,如果我切换2(因此负面答案的代码高于肯定答案的代码),它总会回复,好像我给出了否定回复。

My problem is that even if I reply with a negative answer it will still reply with a response as if I have said yes and if I switch the 2 around (so the code for the negative answer is above the code for the positive answer) it will always reply as if I have given a negative response.

推荐答案

if yearlikedislike in ("yes", "Yes", "YES", "yep", "yup", "Yep", "Yup"):

if yearlikedislike.lower() in ("yes","yep","yup"):

将成功

这篇关于Python - if语句无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆