Why does it say can't assign to literal in my function?

54 views Asked by At

I am trying to make a menu calculator in which the user inputs items and the program will add up the order numbers and output the cost. I have done some of the code already but in the function in says can't assign to literal.

itemlist=["1","2","3","4","5","6","7","8","9"]

def itemcost():
    1=3.50  #can't assign literal error is here
    2=2.50
    3=4.00
    4=3.50
    5=1.75
    6=1.50
    7=2.25
    8=3.75
    9=1.25
return itemcost

order=int(input("Enter order"))

while items in order:
    itemcost+str(order)
2

There are 2 answers

0
Tony Tuttle On

On line 4 (and lines 5-12 after it) the 1 is read as the value one, i.e. a literal value. If you want to assign the value 3.50 to a variable, you will need to name the variable something that cannot be interpreted as a number and does not begin with a number, such as _1 or var1.

0
matrumz On

First, some good information to put in a question is the language and platform you are using. Your error comment in the code IS helpful, however.

What your code is trying to do is assign the value 3.50 to the VALUE 1. You can't change the value of pure numbers for obvious reasons. What I think you want is:

itemlist["1"]=3.50