-2

I am trying to make a list of terms for my chemistry class and be able to print the definition of what I ask for. I.E I ask for the definition of the imperial system and then I get the definition for the term. I have made all my variables and set them to the definitions of the terms but when I try to print a certain definition it prints all the definitions I have.


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)```
  • 2
    `bool("Matter")` is always `True`. Try using `value = input` then `if value == "Matter:"`. To make it even more efficient use a `dict` rather then loose variables so you don't need as many `if`s – mousetail Sep 22 '21 at 08:04
  • Does this answer your question? [Python: how to compare input() string to another string?](https://stackoverflow.com/questions/42185201/python-how-to-compare-input-string-to-another-string) – mkrieger1 Sep 22 '21 at 08:25
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Sep 29 '21 at 18:45

2 Answers2

2

Try Using a dictionary Like this

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"
}

And when you have to access something just use ,

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

For taking use input use

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

or

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
mousetail
  • 7,009
  • 4
  • 25
  • 45
SaGaR
  • 534
  • 4
  • 11
  • 1
    use `terms.get()` for value that input and not exist in dictionary ;) – I'mahdi Sep 22 '21 at 08:18
  • @mkrieger1 This code is just for understanding. Not for actual use and also i fixed it – SaGaR Sep 22 '21 at 08:27
  • 2
    Nevertheless it should at least be syntactically correct. What does it help a beginner to show them invalid code when they are not even able to tell the difference. So they will learn it wrong. – mkrieger1 Sep 22 '21 at 08:28
2

So the other answer is of course what you should be doing. But to clarify the error in your code, you're not assigning the input() to any variable and your if-blocks are always true since they don't really check anything.

Maybe you wanted to do something like this?!

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

if definition == "Imperial":
    print(Imperial)
Standard_101
  • 325
  • 4
  • 14