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.

You are viewing a single thread.
View all comments
6 points

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 **.

permalink
report
reply

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.

permalink
report
parent
reply
4 points
*

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.

permalink
report
parent
reply

Oh, I think I understand now.

Thank you for clarifying that to me!

permalink
report
parent
reply

Python

!python@programming.dev

Create post

Welcome to the Python community on the programming.dev Lemmy instance!

📅 Events
October 2023
November 2023
Past

July 2023

August 2023

September 2023

🐍 Python project:
💓 Python Community:
✨ Python Ecosystem:
🌌 Fediverse
Communities
Projects
  • Pythörhead: a Python library for interacting with Lemmy
  • Plemmy: a Python package for accessing the Lemmy API
  • pylemmy pylemmy enables simple access to Lemmy’s API with Python
  • mastodon.py, a Python wrapper for the Mastodon API
Feeds

Community stats

  • 406

    Monthly active users

  • 145

    Posts

  • 516

    Comments