【问题标题】:How to return values from a function which are iterated upon in a for-loop如何从在for循环中迭代的函数返回值
【发布时间】:2020-04-03 15:55:32
【问题描述】:

我正在尝试编写一个程序,该程序将接受用户的输入(在这种情况下,他们从列表中选择同位素),将其与列表进行匹配,然后打印同位素及其半衰期。这一切都很完美,除了我无法使用与 for 循环之外的输入匹配的半衰期。我尝试将它包含在一个函数中以返回结果,但我所做的一切似乎都不起作用。代码如下:

i=0
iso = list(["Oxygen-22", 2.25, "Carbon-15", 2.45, "Flevorium-289", 2.6, "Oxygen-21", 3.42, "Nitrogen-17", 4.17, "Beryllium-14", 4.84, "Nitrogen-16", 7.13, "Oxygen-20", 13.5, "Beryllium-11", 13.8, "Carbon-10", 19.3, "Oxygen-19", 26.5, "Dubnium-261", 27, "Seaborgium-266", 30, "Dubnium-262", 34]);

print("Please select from the below list of isotopes:");
while i < len(iso):
  print iso[i];
  i = i + 2;

def search(x,y):
  u_iso = input("Enter selection:");
  for x in range(len(iso)):
    if u_iso == iso[x]:
      y = x+1;
      print("Isotope:",iso[x],"Half-life:", iso[y],"seconds");
    else:
      x=x+2;
    if u_iso not in iso:
      x=0;
      u_iso = input("Enter selection again:");
      if u_iso == iso[x]:
        y = x+1;
        print("Isotope:",iso[x],"Half-life:", iso[y],"seconds");
      else:
        x=x+2;
  return x, y;

search(0,0);
print x,y;

【问题讨论】:

    标签: python function return


    【解决方案1】:

    欢迎来到 Stackoverflow。实际上,您并没有保存函数返回的值,这就是您无法使用它们的原因。 如果你想通过函数获取返回值,你可以将它保存在两个变量中,然后你可以打印出来。

    i=0
    iso = list(["Oxygen-22", 2.25, "Carbon-15", 2.45, "Flevorium-289", 2.6, "Oxygen-21", 3.42, "Nitrogen-17", 4.17, "Beryllium-14", 4.84, "Nitrogen-16", 7.13, "Oxygen-20", 13.5, "Beryllium-11", 13.8, "Carbon-10", 19.3, "Oxygen-19", 26.5, "Dubnium-261", 27, "Seaborgium-266", 30, "Dubnium-262", 34]);
    
    print("Please select from the below list of isotopes:");
    while i < len(iso):
      print iso[i];
      i = i + 2;
    
    def search(x,y):
      u_iso = input("Enter selection:");
      for x in range(len(iso)):
        if u_iso == iso[x]:
          y = x+1;
          print("Isotope:",iso[x],"Half-life:", iso[y],"seconds");
        else:
          x=x+2;
        if u_iso not in iso:
          x=0;
          u_iso = input("Enter selection again:");
          if u_iso == iso[x]:
            y = x+1;
            print("Isotope:",iso[x],"Half-life:", iso[y],"seconds");
          else:
            x=x+2;
      return x, y;
    
    p,q = search(0,0);
    print p,q;
    

    如果您有进一步的疑问可以与我分享,我希望它会如您所愿。 ?

    【讨论】:

    • 嘿!感谢您的建议,一旦我从 if 语句中返回 x 和 y 的值,在打印它们之后,这非常有效。在函数末尾执行此操作会为 x 返回一些非常偏离的值。你真的帮我看到了我遇到的问题:)
    • 如果你认为正确,可以在答案中打勾,这将有助于其他人理解。
    • 感谢您的信息!
    【解决方案2】:

    你可以试试这个简单的方法....

    iso =["Oxygen-22","2.25", "Carbon-15","2.45", "Flevorium-289","2.6", "Oxygen- 
    21","3.42", "Nitrogen-17","4.17", "Beryllium-14","4.84", "Nitrogen-16","7.13", 
    "Oxygen-20","13.5", "Beryllium-11","13.8", "Carbon-10","19.3", "Oxygen-19", 
    "26.5", "Dubnium-261","27", "Seaborgium-266", "30", "Dubnium-262", "34"]
    print("Please select from the below list of isotopes: ")
    i=0
    while i < len(iso):
       print(iso[i])
       i = i + 2
    def search():
        u_iso = input("Enter selection: ")
        for x in range(len(iso)):
            if u_iso == iso[x]:
                y = x+1
                    print("Isotope:"+iso[x]+"Half-life:"+ iso[y]+"seconds")
                else:
                    pass (or) print("Isotope not found")
    search()
    

    结果将是..

    Enter selection: Oxygen-22
    Isotope:Oxygen-22Half-life:2.25seconds
    (or)
    Enter selection: Iron
    Isotope not found
    

    【讨论】:

    • 不幸的是,代码似乎无法解决“或”语句。我不知道是我没有安装正确的东西还是什么。无论如何感谢您的帮助,这似乎比我的代码更干净,我会尝试使用您将来提出的建议
    【解决方案3】:

    当您有 Key : Value 对时,使用 Dictionary 总是更好 字典上的文档https://www.geeksforgeeks.org/python-dictionary

    顺便说一句,我把你的代码转换成了字典,

    iso = {}
    
    iso = {"Oxygen-22" : 2.25, "Carbon-15" : 2.45, "Flevorium-289" : 2.6, "Oxygen-21" : 3.42, "Nitrogen-17" : 4.17, "Beryllium-14" : 4.84, "Nitrogen-16" : 7.13, "Oxygen-20" : 13.5, "Beryllium-11" : 13.8, "Carbon-10" : 19.3, "Oxygen-19" : 26.5, "Dubnium-261" : 27, "Seaborgium-266" : 30, "Dubnium-262" : 34}
    
    u_iso = input("Enter selection:");
    
    print(u_iso,end=" ")
    
    print(iso[u_iso])
    

    输出是,

    Enter selection: Oxygen-19
    
    Oxygen-19 26.5
    

    【讨论】:

    • 感谢您重写我的代码,但由于某种原因,我无法打印字典中的键。我想我需要更多地了解字典,然后才能有效地使用它们。感谢您的链接!
    猜你喜欢
    • 1970-01-01
    • 2015-08-08
    • 2016-11-12
    • 2013-04-24
    • 2022-06-15
    • 2019-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多