Python Forum
I can't multiply within my code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I can't multiply within my code
#1
We were given a task at school which means we have to code a program that calculates the highest possible wage for a worker at a toy company. This program requires an inputted value to be multiplied but looking at what I have I can't seem to get it to work because I can input 5 and it will do 5*5 but give me the answer 55555
Here is the code:

print ("Please enter how many teddies you have made today")
a = input()
print ("Please enter how many hours you have worked")
b = input()
A = a * 2
B = b * 5
if A>B:
print ("You are getting paid £",A,"great work today!")
else:
print ("You are getting paid£",B,"nice job!")

Any help is much appreciated - Ryan
Reply
#2
The call to input() returned a string, not an integer. When multiplied with a scalar (number), a string is repeated that many times, see this example. Note the use of quotes to denote a string.

>>> '5' * 5
Output:
'55555'
If you convert your input to an integer, you will obtain the expected result of 25:

>>> int('5') * 5
Output:
25
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020