制作列表并打印定义

2024-04-29 03:33:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在努力为我的化学课列出一个术语表,并且能够打印出我所要求的定义。也就是说,我询问帝国体系的定义,然后我得到了这个术语的定义。我制作了所有变量,并将它们设置为术语的定义,但当我尝试打印某个定义时,它会打印我所有的定义


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)```

Tags: ofthebaseif定义timeonrandom
2条回答

试着用这样的字典

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

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

也许你想做这样的事

definition = input("What would you like to know?\n")

if definition == "Imperial":
    print(Imperial)

相关问题 更多 >