6

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) 
4
  • Can you share the version of python you are using to run this code ? Commented Nov 30, 2014 at 14:57
  • Thats because number is a string here, multiplying a string just concatenates it Commented Nov 30, 2014 at 14:57
  • print('the answer is '+str(number)), input does the conversion to int. Anyways its int(val) or float(val) for decimals Commented Nov 30, 2014 at 14:57
  • stackoverflow.com/questions/3800846/… Commented Nov 30, 2014 at 15:00

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

input doesn't accept strings, i mean if you enter 10 it accepts it as an integer itself.
@Alfie, for Python 2, that is correct. For Python 3, that is not. (See here ). I assumed Python 3 based on the print function utilized.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.