gpt4 book ai didi

python - 我用 python 做的测验不起作用

转载 作者:行者123 更新时间:2023-11-28 21:07:52 25 4
gpt4 key购买 nike

我最近在我的计算机科学课上做了一个测验,但由于某种原因它没有用,我是编程新手,我想知道也许你能帮助我,我知道这会很简单,但正如我所说, 我是新来的。我只复制了 8 个问题中的 4 个问题,但出于某种原因,它忽略了 IF 语句并直接转到 else 语句。代码如下:

score=0
print("Welcome the the general knowledge quiz")
your_name = input("What is your name?: ")

input("Press Enter to Start the Quiz!")

print("1) Who presents Pointless?")
answer = input()
if answer == ["Alexander Armstong", "alexander armstrong", "ALEXANDER
ARMSTRONG"]:
print("Well done", your_name)
score = score+1
else:
print("Sorry, the answer was Alexander Armstrong")
print("Your score is", score)


print("2) Who presents I'm a celeb, get me out of here?")
answer = input()
if answer == ["Ant and Dec", "ant and dec", "ANT AND DEC", "Ant And Dec"]:
print("Well done", your_name)
score = score+1
else:
print("Sorry, the answer was Ant and Dec")
print("Your score is", score)


print("3) What is the capital of England?")
answer = input()
if answer == ["London", "london", "LONDON"]:
print("Well done", your_name)
score = score+1
else:
print("Sorry, the answer was London")
print("Your score is", score)


print("4) Who lives on the White house right now?")
answer = input()
if answer == ["Obama", "obama", "Barack Obama", "barack obama"]:
print("Well done", your_name)
score = score+1
else:
print("Sorry, the answer was Barack Obama")
print("Your score is", score)

最佳答案

answer = input()
if answer == ["Alexander Armstong", "alexander armstrong", "ALEXANDER
ARMSTRONG ):

除了缺少引号,这个 if 语句总是会失败。 input() 总是返回一个字符串,但您正在检查它是否等于一个列表,这永远不可能是真的。如果将 == 替换为 in,它将执行您期望的操作。

此外,您应该考虑使用 .lower() 将答案变成小写,这样您就只有一个值需要检查。

关于python - 我用 python 做的测验不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40983331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com