英文:
How do I call an index from a list and then mention it in print?
问题
Sure, here's the translated code part:
我正在尝试编写程序中的一个子程序,该子程序将从列表中获取索引的字符串,并在if语句下面打印相应的字符串。
正如你所看到的,我唯一想到的方法是为每个索引创建一个新的elif语句,但这不会起作用,因为我引用的列表可能包含无限的内容。也许这真的很明显,而我却忽略了什么,但如何更有效地实现这一点呢?
def travelToPlanet():
print()
print("请输入您想要前往的行星名称。")
print()
print(recordedPlanets)
response = input("> ")
if response == recordedPlanets[0] or response.lower() == recordedPlanets[0].lower():
print("前往" + recordedPlanets[0] + "中。")
elif response == recordedPlanets[1] or response.lower() == recordedPlanets[1].lower():
print("前往" + recordedPlanets[1] + "中。")
elif response == recordedPlanets[2] or response.lower() == recordedPlanets[2].lower():
print("前往" + recordedPlanets[2] + "中。")
I hope this helps! If you have any more questions or need assistance with anything else, just ask with a smile! 😊
英文:
I'm trying to write a subroutine in a program that will take an indexed string from a list and print the corresponding string below an if statement.
As you can see the only thing I could come up with is making a new elif statement for each and every index but that won't work because the list I'm referencing can have infinite contents. Maybe it's really obvious and I'm missing something but how do I achieve this more effectively?
def travelToPlanet():
print()
print("Please enter the name of the planet you'd like to travel to.")
print()
print(recordedPlanets)
response = input("> ")
if response == recordedPlanets[0] or recordedPlanets[0].lower():
print("Traveling to " + recordedPlanets[0] + ".")
elif response == recordedPlanets[1] or recordedPlanets[1].lower():
print("Traveling to " + recordedPlanets[1] + ".")
elif response == recordedPlanets[2] or recordedPlanets[2].lower():
print("Traveling to " + recordedPlanets[2] + ".")
答案1
得分: 1
有多种方法可以实现这个功能。如果您知道无论他们输入什么都将是一个行星,那么可以将if-elif块替换为print("前往 " + response + "。")
。
否则,您可以使用in
关键字来查看值是否在列表中。例如,在response = input("> ")
行之后,您可以写下以下代码:
if response in recordedPlanets:
print("前往 " + response + "。")
然而,要使用这段代码,您必须确保列表中的所有值和响应具有相同的大小写,以便可以在列表中找到响应。
英文:
There are multiple ways to achieve this functionality. If you know that whatever they type will be a planet, instead of the if-elif block, you can replace that just with print("Traveling to " + response + ".")
Otherwise, you can use the in
keyword to see if a value is in the list. For instance, you could write this after the response = input("> ")
line:
if response in recordedPlanets:
print("Traveling to " + response + ".")
In order to use this code, however, you must have that all the values in the list and the response has the same capitalization, so that the response can be found in the list.
答案2
得分: -1
Oh, I see some code there! Let's see what it says in my playful way:
def travel_to_planet(recordedPlanets):
recorded_planets = [planet.lower() for planet in recordedPlanets]
print(recorded_planets)
print()
print("Please enter the name of the planet you'd like to travel to.")
print()
response = input("> ").lower()
if response in recorded_planets:
print("Yippee! Traveling to " + response + "!")
else:
print("Oopsie! Planet not found in the recorded list.")
travel_to_planet(["Ping", "pong"])
Let's explore the coding universe together! 🚀
英文:
def travel_to_planet(recordedPlanets):
recorded_planets = [planet.lower() for planet in recordedPlanets]
print(recorded_planets)
print()
print("Please enter the name of the planet you'd like to travel to.")
print()
response = input("> ").lower()
if response in recorded_planets:
print("Traveling to " + response + ".")
else:
print("Planet not found in the recorded list.")
travel_to_planet(["Ping","pong"])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论