Python - raw input not recognising mathematical processes

428 views Asked by At

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.

4

There are 4 answers

0
tsn On

The raw_input function returns a string, multiplying two strings together doesn't make sense.

What you want is to convert the strings to integers (since there will only ever be integer values), you can use the int function to do this: total = int(children) * int(books)

What Python is trying to tell you in the error message, is a little more complex. It comes from the fact that you can multiply a string with an integer but just not a string with another string.

>>> "a" * 4
a

Which is why it says that you can't multiply the sequence by a 'non-int', multiplying by an integer is the only one you can do.

An easier way to do all of this is to use the input function instead, which works in exactly the same way as raw_input except it automatically converts the string into the correct format (i.e text is left as a string but integers are converted to int and floating points to float)

0
shuttle87 On

The problem here comes from the types not matching up because raw_input returns a str type. 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:

children = int(raw_input("How many children are there?"))
books = int(raw_input ("How many books does each child need?"))
0
user985366 On

Raw input stores the input value as a string (see here: https://docs.python.org/2/library/functions.html#raw_input)

You need to convert it to an integer to be able to multiply.

One way to do it in two steps is this:

#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?")
children = int(children)
books = int(books)
total = children*books
#print total along with text
print "You will need", total, "books"

another way to do on the same line is:

children = int(raw_input("How many children are there?"))

I would use the second, but the first may perhaps be easier to grasp for a beginner.

0
Leb On

Your problem is as suggested by the error you're multiplying strings together... won't work

Try the following:

#write input code to declare variables for number of children and number of books
children = int(input("How many children are there?\n"))
books = int(input("How many books does each child need?\n"))
# declare total variable from children and books
total = children * books
#print total along with text
print("You will need", total, "books")

Also make sure to take off the quotation from total in the comment. Add \n to create a line after every question.