Why that not get the total in python?

Question:

number=input('Enter  a number')
total=number*5
print(total)

This code is not print the total please help me.
This print only the number multiple times.

Asked By: Programaster

||

Answers:

Because input function returns str.

It’s exmplained in documentation: Built-in functions: input.

>>> num = input("enter a number: ")
enter a number: 13
>>> type(num)
<class 'str'>

You need to cast it to int by int(num).

Answered By: ventaquil

If you want to print the number 5 times you got the code correct.
however I think you want to multiply the number by 5.
so convert the number in to integer. Then it will work.

You can check your type of variable by:

print(type(number))

Answer:-

number=input('Enter  a number')
number=int(number)
total=number*5
print(total)

or use in single statement.

number=int(input('Enter  a number'))
total=number*5
print(total)
Answered By: Chamoda De silva

Since input returns a string you have to convert the string to int:

number = input('Enter a number')
total= int(number)*5
print(total)
Answered By: rftr

When you get input from the user using input() it will save the varaible as a string by default. To remedy this you need to convert the string to int like this:

number = int(input('Enter  a number: '))
total = number * 5
print(total)
Answered By: BWallDev

It’s because input returns a string. You need to cast it to int at somewhere. You can try this:

number = int(input('Enter  a number'))
total = number*5
print(total)
#Enter  a number 3
#15
Answered By: mulaixi

When you input a value, it will be saved as a str value. Instead, typecast the value into an integer using int. Note – when you multiply a string by an integer, it duplicates that value by the integer value.

>>> number=input('Enter  a number')
5
>>> print(type(number))
<class 'str'> 
>>> total=number*5
>>> print(total)
'55555'

Instead typecast the input using int()

>>> number=int(input('Enter  a number'))
5
>>> print(type(number))
<class 'int'> 
>>> total=number*5
>>> print(total)
15
Answered By: Anthony Inthavong
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.