-2

I'll take any input. Just started programming in Python and can't solve this error.

My simple code:

hourlyWage = input('Hourly Wage?')
weeklyHours = input('Weekly Hours?')
weeklyWage = hourlyWage * weeklyHours
print(weeklyWage)

The error:

"TypeError: can't multiply sequence by non-int of type 'str'

I create two variables that a User can input, when I try to get the product of the two variables I get an error. Any help is appreciated.

funie200
  • 3,688
  • 5
  • 21
  • 34
Jeff
  • 7
  • 1
  • Use `hourlyWage = int(input('Hourly Wage?'))` etc – ssp Dec 14 '20 at 23:26
  • 2
    Input will always be string. You need to conert it to integer. Use `int(hourlyWage)` – Joe Ferndz Dec 14 '20 at 23:26
  • 6
    Being new is no excuse, your title should always have something to do with your actual question. – Mark Ransom Dec 14 '20 at 23:27
  • https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response : A sking the user for input until they give a valid response I liked this one very much – pippo1980 Dec 14 '20 at 23:44

3 Answers3

0

The input function returns a string, so you need to convert it to a float like this:

hourlyWage = float(input('Hourly Wage?'))
weeklyHours = float(input('Weekly Hours?'))
weeklyWage = hourlyWage * weeklyHours
print(weeklyWage)

Did you notice float()? That converted your string to a float. Now your program will work.

Lakshya Raj
  • 1,669
  • 3
  • 10
  • 34
-1

Convert it to int first. (input() returns a string)

hourlyWage = int(input('Hourly Wage?'))
RvdK
  • 19,580
  • 4
  • 64
  • 107
-3

Edit: disclaimer: You can utilize the information this post provides safely now, proper edits were made after the comments (again).

I should clarify that using eval can be risky in other environments since it allows arbitrary code to be executed. Be sure to check out the commented lines in the code below.

don't use eval in your code. Ever (unless you know what you're doing and have no choice).


By default the input returned from calling input() is a string. Call eval/ float/ int on the input to convert it to a number to be able to perform arithmetic operations on input.

# you could use whatever function you want for conversion, but remember the use-cases for each one
# calling eval on random strings is dangerous in non-testing environments since it can cause any valid code the user wants at all to be run

hourlyWage = float(input('Hourly Wage?')) 
weeklyHours = float(input('Weekly Hours?'))
weeklyWage = hourlyWage * weeklyHours
print(weeklyWage)
Ethicist
  • 791
  • 2
  • 7
  • 23
  • 1
    Yikes don't use eval. Can run code and is dangerous – RvdK Dec 14 '20 at 23:28
  • oops, i assumed it would be used for just the purpose shown in the OP – Ethicist Dec 14 '20 at 23:28
  • Using `eval` on user input is very bad advice. There are better ways to convert an input string to a number. – Mark Ransom Dec 14 '20 at 23:28
  • "Eval is evil". This is a horrible and extremely dangerous way of doing a simple `float(x)` conversion. – FatihAkici Dec 14 '20 at 23:29
  • I've edited to clarify that it was meant to be used only in the provided example. I fear I might be too late judging by the downvotes – Ethicist Dec 14 '20 at 23:29
  • Doesn't matter. `eval` is best avoided at all costs, you don't want to get in the habit of using it. – Mark Ransom Dec 14 '20 at 23:32
  • Right you are, should've kept it in my mind OP is "new" and clarified extensively. Luckily you guys prevented this to be taken as advocation for it's usage. – Ethicist Dec 14 '20 at 23:34
  • The source of the downvotes is telling anyone ever to parse strings into numbers with `eval`. Noting that it's a bad idea doesn't turn it into a good idea. – TigerhawkT3 Dec 14 '20 at 23:45
  • I can't do anything more than say "don't use it" and the exact reason why. OP asked for some ways to do it and that's what I gave him (in a way). Edit: I've made edits that might render this a good idea. – Ethicist Dec 14 '20 at 23:46
  • It's all good. This code is just for myself on my computer, just practicing. I enjoy all the comments. Now I'm trying to figure out how to convert an inputted str to a converted int with rounding. Converting to str worked through. – Jeff Dec 15 '20 at 03:23
  • Round to what exactly? You mean you want to convert the integer you get from calling `int()` on the string to an integer? It's already rounded to the whole number so can you specify how you want to round it (or am I just misinterpreting the question because I haven't slept in a long time) ? – Ethicist Dec 15 '20 at 03:27