带字符串的python if语句

带字符串的python if语句,python,Python,如果以前有人问过这个问题,我很抱歉,但我不知道为什么我的代码不起作用,我需要一些帮助。谢谢(顺便提一下,Python 3.5.1)您不能将变量设置为[…]中的比较表达式。所以 Name=str(input("Your name is? ")) print("Hello,",Name,"!") Age=int(input("And how old might you be? ")) print("So you are",Age,"years old?") print("So on your nex

如果以前有人问过这个问题,我很抱歉,但我不知道为什么我的代码不起作用,我需要一些帮助。谢谢(顺便提一下,Python 3.5.1)

您不能将变量设置为[…]中的比较表达式
。所以

Name=str(input("Your name is? "))
print("Hello,",Name,"!")
Age=int(input("And how old might you be? "))
print("So you are",Age,"years old?")
print("So on your next birthday, you will be",Age+1,"?")
agecorrect=str(input("Yes or no? "))
yes= in ["Yes","yes","Y","y","yes.","Yes."]
no= in ["No","no","N","n","no.","No."]
if agecorrect=yes:
    print("Yes, I was right!")
else
if agecorrect=no:
    realage=int(input("So your real age on your next birthday will be? "))
    print("So you're actually going to be",realage,"? Good to know!")
else
print("I don't understand... I asked for a yes or no answer.")
无效。您只需在列表中设置
yes
no

yes= in ["Yes","yes","Y","y","yes.","Yes."]
如果“是”中的“年龄正确”,则可以使用

yes = ["Yes","yes","Y","y","yes.","Yes."]
no = ["No","no","N","n","no.","No."]

您还丢失了
后面的
否则

检查应始终使用
=
,而不是
=
,这是分配

始终使用

if agecorrect in yes:
    print("Yes, I was right!")
elif agecorrect in no:
    realage=int(input("So your real age on your next birthday will be? "))
    print("So you're actually going to be",realage,"? Good to know!")
else:
    print("I don't understand... I asked for a yes or no answer.")
而不是

if y == 'yes':
如果您编写上面的代码,Python将抛出一个错误,但是其他语言只需将值赋给
y
,然后运行if语句。

这是您的“固定”代码:


错误消息将清楚地告诉您,
yes=in…
in[“yes”、“yes”、“Y”、“Y”、“yes.”、“yes.”]
这是什么?@marounnaroun这是所有可能的答案yes@SirParselot-当然。。但是语法不是有效的变量包含值,而不是语法的部分块。非常感谢,很抱歉出现了错误question@Nezzeh没关系,从错误中吸取教训并改进未来的帖子更重要。
elif
而不是
else if
?@LexyStardust是的,我自己也意识到了这一点,于是就去了。谢谢你,我会记住的谢谢你-而且“你是”是一个陈述,而不是一个问题你也可以做
agecorrect=question in yes
@ForceBru,编辑,谢谢
if y = 'yes':
# don't use uppercase in variables names,
# prefer underscores over camelCase or dashes
# leave space before and after assignment, there are exceptions of this rule
# but not here ;)
name = str(input("Your name is? "))
print("Hello {}!".format(name))
age = int(input("And how old might you be? "))

# english grammar man!
print("So are you {} years old?".format(age))
print("So on your next birthday, you will be {}?".format(age + 1))
agecorrect = str(input("Yes or no? "))

# in is keyword and you use it to check whether item is in collection
# or its not
# please don't try to attach it to variable
# also use space after putting comma
yes = ["Yes", "yes", "Y", "y", "yes.", "Yes."]
no = ["No", "no", "N", "n", "no.", "No."]
if agecorrect in yes:
    print("Yes, I was right!")

# you forgot about colon
elif agecorrect in no:
    realage = int(input("So your real age on your next birthday will be? "))
    print("So you're actually going to be {}? Good to know!".format(realage))
else:
    print("I don't understand... I asked for a yes or no answer.")
yes = ("Yes","yes","Y","y","yes.","Yes.")
question = input("Yes or no? ")
agecorrect = question in yes
if agecorrect:
    # ...