You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python太空主题测验中lower()方法失效问题排查求助

问题解决:lower()方法判断失效的原因及修复方案

你的核心问题很明确:你将用户输入转为小写后,却和首字母大写的目标字符串做比较,导致两者永远不相等,程序只能执行else分支。

比如用户输入"Earth",answerOne.lower()会把它转为"earth",但你对比的是"Earth",这两个字符串显然不匹配,所以判断失败。

需要修正的关键点

所有和lower()处理后的输入做对比的目标字符串,都要改成全小写形式,保持判断逻辑的大小写一致性。

修正后的完整代码

gameGreeting = input("Welcome to the space quiz! What is your name? ")
print(f"Nice to meet you, {gameGreeting}!")
gameStart = input(f" Whenever you are ready to begin, please type 'start' : ")

if gameStart.lower() != 'start':
    print("Invalid input. Thanks for trying.")
    quit()

print("Great let's begin!")
score = 0

answerOne = input("Let's start off easy, what planet do we live on? ")
# 目标字符串改为全小写
if answerOne.lower() == 'earth':
    print("Correct! At least you can say you aren't an alien...probably.")
    score += 1
    print("Next question!")
else:
    print("Incorrect. You are being judged heavily right now.")
    print("Next question.")


print("Let's make things a little bit more difficult.")
answerTwo = input("What is the diameter of the Sun? ")

# 此处无字母,不影响判断,保持原写法即可
if answerTwo.lower() == "864,000 miles" or answerTwo.lower() == "864000 miles":
    print("Correct! Kudos to you if you didn't google the answer!")
    score += 1
    print("Three more to go! Next question!")
else:
    print("Incorrect. You're 864,000 miles away from winning this quiz.")
    print("Next question.")

answerThree = input("What is the sixth planet away from the Sun? ")
# 目标字符串改为全小写
if answerThree.lower() == "saturn":
    print("Correct! Wow, you're pretty good at this!")
    score += 1
    print("Next question!")
else:
    print("Incorrect. Perhaps you should've paid more attention in science class.")
    print("Next question.")
  
answerFour = input("What are the four gas giants of our solar system in order? ")
# 目标字符串改为全小写
if answerFour.lower() == "jupiter, saturn, uranus and neptune" or answerFour.lower() == "jupiter saturn uranus neptune":
    print("Correct! You are on a roll!")
    score += 1
    print("Here's the last question!")
else:
    print("Incorrect. Hey we can't all be winners.")
    print("Here's the last question.")

answerFive = input("True or False: The process of an object being compressed vertically and/or horizontally in a powerful gravitational field is called Spagettification." )
# 目标字符串改为全小写
if answerFive.lower() == "true":
    print("Correct!! You are truly the master of the Milky Way!")
    score += 1
else:
    print("Incorrect. There's always next time! Try again!")

print("You got " + str(score) + " out of 5 questions correct!")
print("Your total score is " + str((score /5) * 100) + "%! Thank you for playing!")

额外优化建议(可选)

可以在获取输入时增加strip()方法去除首尾空格,比如answerOne.strip().lower(),避免用户输入前后空格导致的判断失败。

内容的提问来源于stack exchange,提问作者kerropileaaf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 08:12:44