为什么我收到这个如果else语句“不正确”的输出?

问题描述 投票:0回答:3

我是新来的蟒蛇,并试图通过做小项目来学习。

我想写显示四个属性的名称,并询问用户,以确定是不是铁路财产的程序。如果选择的正确与否,用户应该是明智的。

properties = "Reading,","Pennsylvania","B & O","Short Line"
question = str(input("Which is not a railroad?")) **Short Line**
if properties == "Short Line":
    print("correct")
else:
    print("incorrect")

然而,我最终输出显示为“不正确”,我在做什么错?

这四个铁路属性阅读,宾夕法尼亚州,B&O,和短线。这是不是一条铁路?短线正确的。短线是一个公交公司。

python if-statement condition boolean-logic
3个回答
0
投票

两件事情我看到这个代码您已发布。

首先,不知道你是否确实有**Short Line**在您的实际代码,但如果你想发表评论使用#这样就不会在运行时进行解释。

其次,如要检查针对该阵列中拉属性的其他的答案中提到。您应该检查对您输入这是保存在的问题。

properties = "Reading,","Pennsylvania","B & O","Short Line"
question = str(input("Which is not a railroad?")) # **Short Line**
if question == "Short Line": # replaced properties with question
    print("correct")
else:
    print("incorrect")
print(properties)
print(question)

我发现,当我有麻烦理解为什么有些东西不工作我抛出一些打印语句,看看有什么变数都在做。


0
投票

您可能要赶在一个循环的用户,否则你就必须不断地有运行代码,找到正确的答案(除非这是所需的行为,那么你可以把它作为你有的话)。此外,要知道,你可能要大写或小写,因为用户可以提供答案为“短线”(小写字母“L”),代码将返回为不正确。当然,这取决于你将接受一个答案。

样品

print ("Reading,Pennsylvania,B & O, or Short Line. Which is not a railroad?")
user_input = input("Please provide an answer: ")
# != the loop will close once the user inputs short line in any form
# The upper.() will convert a user_input string to all caps 
while user_input.upper() != "SHORT LINE":
  print ("Incorrect, Please try again.")
  user_input = input("Which one is not a railroad? ")

print ("Correct")

-1
投票

Prettied它为你

print( "Reading, Pennsylvania, B & O, and Short Line. Which is not a railroad?" )
print("Which is not a railroad?")
answer = input()
if answer == "Short Line":
    print("correct")
else:
    print("incorrect")
© www.soinside.com 2019 - 2024. All rights reserved.