-3
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.

6 Answers6

1

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
0

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).

ventaquil
  • 2,780
  • 3
  • 23
  • 48
0

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)
0

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

number = input('Enter a number')
total= int(number)*5
print(total)
rftr
  • 1,185
  • 2
  • 10
  • 19
0

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)
BWallDev
  • 345
  • 3
  • 13
0

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
mulaixi
  • 183
  • 2
  • 11