如何从列表中调用索引,然后在打印中提到它?

huangapple go评论63阅读模式
英文:

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"])

huangapple
  • 本文由 发表于 2023年7月18日 11:56:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709435.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定