0

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?

Ted Klein Bergman
9,8175 gold badges35 silver badges61 bronze badges
asked May 9, 2021 at 15:45

6 Answers 6

2

By default, input() returns a string. Using

a = int(input("Enter a number: "))

Should work.

answered May 9, 2021 at 15:47
Sign up to request clarification or add additional context in comments.

Comments

1

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.

answered May 9, 2021 at 15:48

2 Comments

You can multiply a string though. It'll just repeat the string b amount of times, which is what happened to the asker.
Yes I know.I said this is possible. But your output is incorrect
0

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.

answered May 9, 2021 at 15:51

Comments

0

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
answered May 9, 2021 at 16:27

Comments

0

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)
answered May 9, 2021 at 16:38

Comments

0

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)
answered May 9, 2021 at 16:51

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.