Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python convert a string to a integer for multiplication [duplicate]

Tags:

python

I realise that this is probably simple and basic to you guys, its for my childs homework and I don't have any experience or idea on the use of Python. She needs to get the code to request a number, multiply it by 9 and show the result. She is using the code below, however it repeats the number rather than multiply it. (ie it show 3 * 9 as 999 instead of 27). From what I have read it appears this is something to with multiplying integer by strings (though I may be wrong). Any help would be greatly appreciated.

number=input("Enter a number to multiply by 9 ")
number=number*9
print('the answer is '+number)
like image 783
Steve Keeley Avatar asked Nov 30 '14 14:11

Steve Keeley


1 Answers

Wrap your input in either int or float

number=int(input("Enter a number to multiply by 9 "))

or

number=float(input("Enter a number to multiply by 9 "))

This is done because input accepts strings and you have to convert these to numerals.

like image 76
Andy Avatar answered Oct 02 '22 14:10

Andy