I'm a primary teacher - I'm using python v 2.7 in ICT. I'm using a standard scheme of work provided by 3rd party company. It's been great so far. We've moved onto raw input and hit a snag, Program as follows:
#write input code to declare variables for number of children and number of books
children = raw_input("How many children are there?")
books = raw_input ("How many books does each child need?")
# declare ‘total’ variable from children and books
total = children*books
#print total along with text
print "You will need", total, "books"
Error message as follows:
File "C:\Python34\ss.py", line 6, in <module>
total = children*books
TypeError: can't multiply sequence by non-int of type 'str'
No matter what I do, I can't get the variables to perform any kind of mathematical operation. Any advice out there? I'd love to get my kids writing a program like this.
The problem here comes from the types not matching up because
raw_inputreturns astrtype. It does this even if you entered in something that appeared to be a number to it. So essentially you are trying to multiply 2 strings together which doesn't make sense, hence the error you are getting. To properly deal with this you need to integers first: