I’m currently doing Dr. Charles Severence’s lessons on FreeCodeCamp to try to learn Python3. I’m on lesson exercise 02_03 and confused about multiplying floating-point and integer values.

The goal is to write a Python program multiplying hours worked by pay rate to come up with a pay quantity.

This is the code I wrote:

h = input("Enter hours: ")
r = input("Enter pay rate: ")
p = float(h) * r

I got a traceback error, and the video said the correct way to solve said error was change Line 3 from p = float(h) * r to p = float(h) * float(r).

However, what I’m confused about is why would I need to change r to a floating-point value when it’s already a floating-point value (since it’d be a currency value like 5.00 or something once I typed it in per the input() command*?

What am I missing here?

 


*I can’t remember: are the individual commands in a python line called “commands”?

 

 


Edit: Wrote plus signs in my post here instead of asterisks. Fixed.

 


EDIT: Thanks to @Labna@lemmy.world and @woop_woop@lemmy.world. I thought that the input() function was a string until the end-user types something in upon being prompted, and then becomes a floating-point value or integer value (or stays a string) according to what was typed.

This is incorrect: the value is a string regardless of what is typed unless it is then converted to another type.

  • woop_woop@lemmy.world
    link
    fedilink
    arrow-up
    12
    ·
    edit-2
    2 months ago

    The traceback should give you an idea of what’s going on, but you can test for yourself by checking the result of input:

    test = input('enter number:')
    print(type(test))
    

    Another question to ask is “why did you cast ‘h’ as a float?” And what happens if you just do h + r?

    • EveryMuffinIsNowEncrypted@lemmy.blahaj.zoneOP
      link
      fedilink
      English
      arrow-up
      4
      ·
      edit-2
      2 months ago

      Honestly, I had a bunch of little confusions. I thought the input() function was only a string until the user typed in a value when prompted, and then it became either an integer value or a floating-point value depending on what you typed in.

      Thanks to Labna@lemmy.world and your other response, I understand that it is always a string regardless until you convert it after the fact.

      Also, I meant to type an asterisk instead of a plus sign when typing over my code snippet into my post. Fixed now.

      Also, to answer your last question, if I do h+r or h*r, I get “5010” for the former (which makes sense) and the standard “can’t multiply sequence by non-int of type ‘str’”, which also makes sense to me now that I understand the above point.

      • woop_woop@lemmy.world
        link
        fedilink
        arrow-up
        5
        ·
        2 months ago

        That’s fair, I was trying to be a bit vague since you’re learning and wanted to help point you to the solution. Went a little too vague with it 🙂

        • EveryMuffinIsNowEncrypted@lemmy.blahaj.zoneOP
          link
          fedilink
          English
          arrow-up
          2
          ·
          2 months ago

          I really appreciate the effort! My dream is to eventually learn enough from free online courses to then take a certification test and then maybe I can get a job even though I don’t have a degree. I fear my lack of a degree will doom that goal before I ever get a chance, but I have to take that chance, I feel. Also, I fucking hate customer service. Lol.

          • woop_woop@lemmy.world
            link
            fedilink
            arrow-up
            2
            ·
            2 months ago

            Eh, degrees can be overrated. I don’t have one and it hasn’t hindered me at all. Ultimately, it depends what kind of work you want to get into and your drive to self learn, how quick you can pick things up, and adaptability. You got this.

            • EveryMuffinIsNowEncrypted@lemmy.blahaj.zoneOP
              link
              fedilink
              English
              arrow-up
              2
              ·
              edit-2
              2 months ago

              I don’t mind self-learning. Hell, if I’m interested in the subject matter, I usually find myself experimenting and researching.

              I’m all about that “wait, I wonder if…” mindset. 😎

          • BangersAndMash@lemmy.world
            link
            fedilink
            arrow-up
            2
            ·
            edit-2
            2 months ago

            I’ve been hiring programmers recently. A year or two experience wins out over a degree almost any time. I don’t know if there are any developers (or even website administrators) at you current job, but if there are I’d see you if you can start by helping them out, maybe helping out when they’re on leave or picking up little jobs and then you’ll get your foot in the door in no time at all.

            Actually the last dev I hired had no experience at all, I just really liked him, and he’s turned out brilliantly.

            • EveryMuffinIsNowEncrypted@lemmy.blahaj.zoneOP
              link
              fedilink
              English
              arrow-up
              1
              ·
              edit-2
              2 months ago

              I work as a cashier at a dead-end retail store in a town of 5000. (Seriously, the closest reasonably large city is like 30 minutes away.) So I don’t think there’s much of an opportunity at my current workplace. Haha.

              But you still make an excellent point and it sounds like a good starting-off point. Thank you!

  • Labna@lemmy.world
    link
    fedilink
    arrow-up
    6
    ·
    2 months ago

    I think I understand where you get confused. The returned value of the input function is always a string, you have to convert it into a number before using it in calculation. Otherwise the auto-parser will convert everything into string. Even if you use * or **.

    • EveryMuffinIsNowEncrypted@lemmy.blahaj.zoneOP
      link
      fedilink
      English
      arrow-up
      1
      ·
      2 months ago

      But I thought the “value” doesn’t exist until the end-user types in the value, due to the use of input(). So it starts off as a string, then becomes whatever is typed in, which then gets filtered through the next line. So if I type 3, it’ll be considered as an integer, and likewise as a float if I type 3.00.

      • woop_woop@lemmy.world
        link
        fedilink
        arrow-up
        4
        ·
        edit-2
        2 months ago

        the signature for the input function (that’s what it’s called instead of command) is

        def input(__prompt: Any = ...) -> str
        

        which means it’s always going to return a string.

        So it starts off as a string, then becomes whatever is typed in

        there’s no real way for something to do that automatically without a much more robust setup.

        this snippet proves that

        test_int = input('enter integer:')
        print(type(test_int))
        test_float = input('enter float:')
        print(type(test_float))
        test_str = input('enter string:')
        print(type(test_str))
        
        >> <class 'str'>
        >> <class 'str'>
        >> <class 'str'>
        

        it is the responsibility of your program to validate and do whatever you want with the result, and part of that can include casting it to a different type.