0

I have this prompt:

You have to do your chemistry homework, but you hate looking up elements on the periodic table! Write a program that takes the name of an element (as a string, independent of case) from standard input and prints a double representing its atomic weight to standard output. Only implement the program for the first three elements, hydrogren, helium, and lithium, which have the respective atomic weights 1.008, 4.0026, and 6.94. If anything else is given as input, print the statement "Sorry, I don't recognize that element!"

I wrote a code but keep getting wrong results. It doesn't matter what I type, it always comes up wrong. The standard output keeps stating "Enter element name:" along with atomic weight number

Here's what I wrote:

hydrogen = 1.008
helium = 4.0026
lithium = 6.94
atomic_weight = input("Enter element name:").lower()
if atomic_weight == hydrogen:
    print(hydrogen)
elif atomic_weight == helium:
    print(helium)
elif atomic_weight == lithium:
    print(lithium)
else:
    print("Sorry, I don't recognize that element!")

Updated code

hydrogen = 1.008
helium = 4.0026
lithium = 6.94
element_name = input("Enter element name:").lower()
if element_name == hydrogen:
    print(hydrogen)
elif element_name == helium:
    print(helium)
elif element_name == lithium:
    print(lithium)
else:
    print("Sorry, I don't recognize that element!")
Paul Bernella
  • 31
  • 1
  • 4
  • 13
  • `atomatic_weight` is a string; that's what you'll get back from `input` (otherwise `.lower()` wouldn't work. To compare a string with a float (because you're elements are floats), you'll have to convert the string to float. Additional note: floating point comparison are imprecise. –  Sep 18 '15 at 02:15
  • It would be helpful to us, but even more helpful to you to explain what "wrong results" you were getting. If you had put "No matter what name I enter, I get 'Sorry…'" you might have seen the problem. Also naming variables properly could have save you trouble; you are not asking for an `atomic_weight` which we know is a number. You asked for an `element_name` which would make a much better name for the input string. – msw Sep 18 '15 at 02:21
  • Thanks for the naming suggestion! – Paul Bernella Sep 18 '15 at 02:38
  • Possible duplicate of [How can I read inputs as integers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-integers) – tripleee Mar 11 '18 at 10:37

4 Answers4

4

You're comparing your input to the atomic weights. You probably want to compare it to strings, e.g. "hydrogen", "helium" and "lithium".

Max Noel
  • 8,810
  • 1
  • 27
  • 35
1

I finally figured it out, the input was supposed to be empty

hy = 1.008
he = 4.0026
li = 6.94


element_name = input("").lower()

if element_name == "hydrogen":
    print(hy)
elif element_name == "helium":
    print(he)
elif element_name == "lithium":
    print(li)
else:
    print("Sorry, I don't recognize that element!")
Paul Bernella
  • 31
  • 1
  • 4
  • 13
  • 1
    If you mean the string inside `input("")`; no, that doesn't matter. Anything you'll put there will be used a string (question) before the actual input given by someone. It's perfectly fine to have `"Enter element name: "` there. –  Sep 19 '15 at 06:15
0

You probably want a dictionary, if you're trying to access the atomic weight using the name:

elements = {'hydrogen': 1.008,
            'helium': 4.0026,
            'lithium': 6.94}

name = input("Enter element name:").lower()
print(elements.get(name, 'element not found!'))
  • Of course this is the more Pythonic way to do it, but consider your audience. If you are going to answer at all, be aware that you are throwing dictionaries and an atypical access mode (`get`) at someone who is having trouble groking types and other rudiments. – msw Sep 18 '15 at 02:28
0

Thanks for all who shared above, wrote my version and it works (my first stack overflow post):

    hydrogen = 1.008
    helium = 4.0026
    lithium = 6.94
    element_name = input()
    name=element_name.lower()

    if name == "hydrogen":
        print(hydrogen)
    elif name == "helium":
        print(helium)
    elif name == "lithium":
        print(lithium)
    else:
        print("Sorry, I don't recognize that element!")