Reputation: 1
I'm new to python and need your help in this issue. Tried looking everywhere but didn't find any working solution.
My code is:
a=input("Enter a number : ")
b=10
c=a*b
print("Multiply =",c)
The output I'm getting for a=2
is:
Multiply = 2222222222
I want it to print Multiply = 20
but instead it is printing 10 times the digit 1, but I want the product. How do i solve this?
Upvotes: 0
Views: 150
Reputation: 183
Remember in python input() function always returns string so when you need any certain data type as your input type cast accordingly, Here are few Examples
x = int(input()) # input 20
print(x) -> prints 20
x = float(input()) # input 50
print(x) -> prints 50.0
x = list(map(int, input().split(' '))) # input 20 30 40 as integer
print(x) -> prints list [20, 30, 40]
so the solution for your question is:
a=int(input("Enter a number : "))
b=10
c=a*b
print("Multiply =",c)
Upvotes: 0
Reputation: 56
Input takes string. So first of all convert it to integer by calling int() function
a = int(input("Enter a number : "))
b = 10
c = a*b
print("Multiply =",c)
Upvotes: 0
Reputation: 25
You Can Use float
to add integer or float as input.
print("Multiply =", float(input("Enter a number : "))*10)
int1: 5
int2: 5.5
out1: 50.0
out2: 55.0
Or:
print("Multiply =", int(float(input("Enter a number : "))*10))
int1: 5
int2: 5.5
out1: 50
out2: 55
Upvotes: 0
Reputation: 1404
You need to convert the input to an integer using something like int(input("Enter a number : "))
but if the input is not an integer that will cause issues.
To make sure the input is a string I recommend something like this:
while True:
a = input("Enter a number : ")
if a.isnumeric():
a = int(a)
break
print("You did not input a number.")
b = 10
c = a * b
print("Multiply =",c)
This will loop until the user gives a number.
Upvotes: 0
Reputation: 5889
You can't multiply a string. I like to think of it as 10*'word'
. Possible but won't get you an output.
a=int(input("Enter a number : "))
b=10
c=a*b
print("Multiply =",c)
Take an int input.
Upvotes: 1
Reputation: 1373
By default, input()
returns a string. Using
a = int(input("Enter a number: "))
Should work.
Upvotes: 2