2

Hi I am new to python and I am practicing by making a simple calculator. The program lets me input numerical values for meal, tax, and tip but when doing the calculation I get this error:

Traceback (most recent call last):
 File "C:/Users/chacha04231991/Desktop/pytuts/mealcost.py", line 5, in <module>
 meal = meal + meal * tax
TypeError: can't multiply sequence by non-int of type 'str'

The is the code:

meal = raw_input('Enter meal cost: ')
tax = raw_input('Enter tax price in decimal #: ')
tip = raw_input('Enter tip amount in decimal #: ')
meal = meal + meal * tax
meal = meal + meal * tip
total = meal
print 'your meal total is ', total
avasal
14.9k4 gold badges33 silver badges49 bronze badges
asked Dec 20, 2012 at 10:40
1
  • 2
    result of the raw_input is str. Commented Dec 20, 2012 at 10:41

5 Answers 5

1

You need to convert your inputs from strings to numbers, e.g. integers:

meal = int(raw_input('Enter meal cost: '))
tax = int(raw_input('Enter tax price in decimal #: '))
tip = int(raw_input('Enter tip amount in decimal #: '))

You could also use the decimal type if you need to enter fractional monetary amounts.

from decimal import Decimal 
meal = Decimal(raw_input('Enter meal cost: '))
tax = Decimal(raw_input('Enter tax price in decimal #: '))
tip = Decimal(raw_input('Enter tip amount in decimal #: '))

I would advise you not to use floats for this because it will give rounding errors.

answered Dec 20, 2012 at 10:41
Sign up to request clarification or add additional context in comments.

2 Comments

It works fine using float but when I try decimal is gives this error: Traceback (most recent call last): File "C:/Users/chacha04231991/Desktop/pytuts/mealcost.py", line 4, in <module> tax = decimal(raw_input('Enter tax price in decimal #: ')) NameError: name 'decimal' is not defined
@codeYah: decimal with a small d is the name of the module. The type Decimal has a capital D.
1

when you use raw_input, the input you get is of type str

>>> meal = raw_input('Enter meal cost: ')
Enter meal cost: 5
>>> type(meal)
<type 'str'>

you should convert it to int/float before performing action

>>> meal = int(raw_input('Enter meal cost: '))
Enter meal cost: 5
>>> type(meal)
<type 'int'>
answered Dec 20, 2012 at 10:42

Comments

0

Input is a string by default in python. You will have to convert it to an integer before multiplying.

int(meal)
int(tax)
int(tip)

should do the trick.

Parse String to Float or Int

answered Dec 20, 2012 at 10:44

Comments

0

This is simple and also I wrote this code depending on the user operand input as string..

def calculator(a, b, operand):
 result = 0
 if operand is '+':
 result = a + b
 elif operand is '-':
 result = a - b
 elif operand is '*':
 result = a * b
 elif operand is '/':
 result = a / b
 return result
calculator(2, 3, '+')
output -> 5
answered Dec 30, 2018 at 4:15

Comments

-1

By default, python assumes all input as string value. So we need to convert string values to numbers through either float or int.

answered Jun 4, 2022 at 7:56

1 Comment

This has already been mentioned in the other answers.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.