[英]How to take user input as function input
I am trying to make my function take an name from the user which would check if the name is in a whitelist before executing a function that prints draws out information from a pre-defined list of the same name but the entered input is being processed as a string by the function instead of the name of the list. 我试图让我的函数从用户那里取一个名字,该名字将在执行一个函数时检查该名字是否在白名单中,该函数从具有相同名字的预定义列表中提取信息,但是输入的输入将被处理为函数的字符串,而不是列表的名称。 How do I get it to take in the input as the name of the list? 如何获取输入内容作为列表的名称?
hydrogen = ["Hydrogen", "H", "1", "1.0"]
helium = ["Helium", "He", "2", "4.0"]
universe = ["hydrogen", "helium"]
elementname_print = "Element Name: "
elementsymbol_print = "Element Symbol: "
atomicnumber_print = "Atomic Number: "
relativeatomicmass_print = "Relative Atomic Mass: "
def printelement(element):
print(f" \n-------------------------")
print(elementname_print + element[0])
print(elementsymbol_print + element[1])
print(atomicnumber_print + element[2])
print(relativeatomicmass_print + element[3])
print("-------------------------")
userinput = input("-->")
if userinput in universe:
printelement(userinput)
else:
print("Sorry that element cannot be found.")
Result: 结果:
--> hydrogen ->氢
Element Name: h 元素名称:h
Element Symbol: y 元素符号:y
Atomic Number: d 原子数:d
Relative Atomic Mass: r 相对原子质量:r
You should, rather than defining your elements in global scope as hydrogen = ...
, define them inside a dictionary keyed by their name. 您应该在全局范围内将元素定义为以它们的名称为键的字典中,而不是将它们定义为hydrogen = ...
elements = {"hydrogen": ["Hydrogen", "H", "1", "1.0"],
"helium": ["Helium", "He", "2", "4.0"]}
the lookup then becomes much easier. 查找变得容易得多。
def print_element(element_name):
element = elements[element_name]
# the rest as written
Note that you can clean up your code quite a bit: 请注意,您可以清理代码很多:
elements = {"hydrogen": ["Hydrogen", "H", "1", "1.0"],
"helium": ["Helium", "He", "2", "4.0"]}
def print_element(element_name):
element = elements[element_name]
name, symbol, number, mass = element
print(f"""
----------------------
Element Name: {name}
Element Symbol: {symbol}
Atomic Number: {number}
Relative Atomic Mass: {mass}
----------------------""")
userinput = input("-->")
if userinput in elements:
print_element(userinput)
else:
print("Sorry that element cannot be found.")
There are ways to make your chosen solution work ( eval
will do it, but introduce huge security risks. globals()
will do it, but introduce a large performance overhead), but they're all ugly. 有多种方法可以使您选择的解决方案起作用( eval
可以做到,但是会带来巨大的安全风险globals()
可以做到,但是会带来很大的性能开销),但是它们都很难看。 Writing an ugly hack is objectively worse than using the right approach in the first place 从客观上讲,编写丑陋的hack比使用正确的方法更糟糕
You can eval the string input to the corresponding variable : 您可以评估输入到相应变量的字符串:
printelement(eval(userinput))
Rest code remains the same. 其余代码保持不变。
PS : This is a quick hack, using eval is unsafe. PS:这是一个快速的技巧,使用eval是不安全的。
Basically your need to get a list corresponding to the user input. 基本上,您需要获取与用户输入相对应的列表。 Use globals()
: 使用globals()
:
lst = globals()[userinput]
So in your example, if user types in 'hydrogen'
, this will give the list hydrogen
. 因此,在您的示例中,如果用户输入'hydrogen'
,则将给出列表hydrogen
。 Now do your printings. 现在开始打印。
Complete example : 完整的例子 :
hydrogen = ["Hydrogen", "H", "1", "1.0"]
helium = ["Helium", "He", "2", "4.0"]
universe = ["hydrogen", "helium"]
elementname_print = "Element Name: "
elementsymbol_print = "Element Symbol: "
atomicnumber_print = "Atomic Number: "
relativeatomicmass_print = "Relative Atomic Mass: "
def printelement(element):
print(f" \n-------------------------")
print(elementname_print + element[0])
print(elementsymbol_print + element[1])
print(atomicnumber_print + element[2])
print(relativeatomicmass_print + element[3])
print("-------------------------")
userinput = input("-->")
if userinput in universe:
lst = globals()[userinput]
printelement(lst)
else:
print("Sorry that element cannot be found.")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.