我是初学者。就像-我几乎不知道如何编码。我什至不确定我是否问过这个问题或是否正确嵌入了代码。基本上,我刚刚做了一个小测验。最后一个问题可以接受用户输入的答案,并且只需要包含列表中的一个元素即可被视为正确或不正确
five_ans = input ("What is the answer?")
list_q5_mark1 = ["answercondition1", "another way to write answercondition1"]
list_q5_mark2 = ["answercondition2", "another way to write answercondition2"] #the 'another way to write' is just to ensure the user doesn't get an false incorrect. It's the same thing, without spaces.
if list_q5_mark1 in five_ans:
print ("correct! it was also answercondition2")
elif list_q5_mark2 in five_ans:
print ("correct! it was also answercondition1")
elif list_q5_mark1 and list_q5_mark2 in five_ans:
print ("correct! you got both")
else:
print ("incorrect")
我想让用户可以输入任何内容,并且只要包含列表中的一项,它就是正确的。因此,他们可以输入“答案是‘answercondition1’”,它会给出第一个打印语句,例如。
这是我认为有效地做到这一点的唯一方法。然而,当我运行此命令时,它会提出问题,但任何答案都会给出此错误:
'类型错误:'in '需要字符串作为左操作数,而不是列表'
我对Python真的很陌生,所以请尝试用白痴的语言解释一下,哈哈。
我对你的代码做了一些修改,它工作得很好,我想我的意思是根据你的要求。
Five_ans1 = 输入(“答案是什么?”)
Five_ans2 = 输入(“答案是什么?”)
list_q5_mark1 = ["answercondition1", "answercondition1的另一种写法"] list_q5_mark2 = ["answercondition2", "answercondition2的另一种写法"] #'另一种写法'只是为了确保用户不会得到错误的错误。都是一样的,没有空格。
如果 Five_ans1 和 list_q5_mark1 位于 list_q5_mark2 中的 Five_ans2 中: print (“正确!你两者都有”) list_q5_mark1 中的 elif Five_ans1: print ("正确!这也是答案条件2") list_q5_mark2 中的 elif Five_ans2: print ("正确!这也是answercondition1") 别的: 打印(“不正确”) 正确保留缩进并检查一下。