创建列表并打印定义



我正试着为我的化学课制作一个术语列表,并能够打印出我所要求的定义。也就是说,我问帝制的定义,然后我得到了这个术语的定义。我已经创建了所有变量,并将它们设置为术语的定义,但是当我试图打印某个定义时,它会打印我拥有的所有定义。


VolumeUnits = "gal, cup, table spoons, teaspoons, quart, ounces, pints"
MetricSystem = "Based on the meter, really just a stick, Based on powers of 10"
PreFixes = """kilo 
hecto 
deka 
base 
deci 
centi 
mili"""
Mass = "the amount of matter in an object, base unit is kilograms"
Weight = "the pull of gravity on a mass"
Volume = "lenght x width x height"
Random = "1L - dm^3.  1ml - cm^3"
Time = "base is seconds"
Temperature = "Celsius, kelvin"
SigFig = "the last number that can be measured with confidence"
Chemistry = "the study of matter and its properties and reactions"
Matter = "anything that takes up space and has mass"
input("What would you like to know?n")

if "Imperial":
print(Imperial)

if "VolumeUnits":
print(VolumeUnits)

if "MetricSystem":
print(MetricSystem)

if "PreFixes":
print(PreFixes)

if "Mass":
print(Mass)

if "Weight":
print(Weight)

if "Volume":
print(Volume)


if "Random":
print(Random)

if "Time":
print(Time)

if "Temperature":
print(Temperature)

if "SigFig":
print(SigFig)

if "Chemistry":
print(Chemistry)

if "Matter":
print(Matter)```

另一个答案当然是你应该做什么。但是为了澄清代码中的错误,您没有将input()分配给任何变量,并且您的if块始终为真,因为它们并不真正检查任何内容。

也许你想做这样的事情?!
definition = input("What would you like to know?n")
if definition == "Imperial":
print(Imperial)

尝试像这样使用字典

terms = {
'mass' : "the amount of matter in an object, base unit is kilograms",
'weight' : "the pull of gravity on a mass",
'volume' : "lenght x width x height"
}

当你需要访问某些东西时,只需使用

terms[anyterm] #example terms['volume']

使用

term = input('What you want to know more about')
print(terms[term]) # Notice no quotes to use a variable

print(terms.get(term, 'Term not found')) # Second argument is to
# ensure that if term is not in dictionary then this will be the default value

最新更新