I need help to identify the problems in this code

price = [5.50, 1.99, 35.00, 26.52]
price1 = 0
price2 = 0
price3 = 0
price4 = 0

#first item
item1 = float(input("Choose ID from: 10.0, 11.1, 20.3, 6.4. "))
if item1 == 10.0 or 10:
  item_number1 = int(input("How many of these do you need? "))
  price1 = price[0] * item_number1
  go_ON = str(input("Do you want to continue? "))
  if go_ON == "no":
    print("Your total cost is:", price1 + price2 + price3 + price4)
  elif go_ON == "yes":
    print("Choose another ID from: 10.0, 11.1, 20.3, 6.4. ")
    item2 = float(input("Choose ID from: 10.0, 11.1, 20.3, 6.4. "))

#second item
if item2 == 11.1:
  item_number2 = int(input("How many of these do you need? "))
  price1 = price[1] * item_number2
  go_ON = str(input("Do you want to continue? "))
  if go_ON == "no":
    print("Your total cost is:", price1 + price2 + price3 + price4)
  elif go_on == "yes":
    print("Choose another ID from: 10.0, 11.1, 20.3, 6.4. ")
    item3 = float(input("Choose ID from: 10.0, 11.1, 20.3, 6.4. "))

#third item
if item3 == 20.3:
  item_number3 = int(input("How many of these do you need? "))
  price1 = price[2] * item_number3
  go_ON = str(input("Do you want to continue? "))
  if go_ON == "no":
    print("Your total cost is:", price1 + price2 + price3 + price4)
  elif go_ON == "yes":
    print("Choose another ID from: 10.0, 11.1, 20.3, 6.4. ")
    item4 = float(input("Choose ID from: 10.0, 11.1, 20.3, 6.4. "))

#fourth item
if item4 == 6.4:
  item_number4 = int(input("How many of these do you need? "))
  price4 = price[3] * item_number4
  print("Your total cost is:", price1 + price2 + price3 + price4)

#error
if item1 != 10.0 or 10:
  print("Error.")
elif item2 != 11.1:
  print("Error.")
elif item3 != 20.3:
  print("Error.")
elif item4 != 6.4:
  print("Error.")

#end
print("Your total is:", price1 + price2 + price3 + price4)

Maybe it would be better to say what issues you are having, rather than have us guess.

One thing I do see right away is code repetition; the list of numbers: 10.0, 11.1, 20.3, 6.4 which would be better stored in a list[] object.

Also, you don’t need 10.0 or 10: they both evaluate to 10.

Another problem is attempting to look up values of undefined variables. Consider what happens if the user enters "no" here:

  go_ON = str(input("Do you want to continue? "))
  if go_ON == "no":
    print("Your total cost is:", price1 + price2 + price3 + price4)
  elif go_ON == "yes":
    print("Choose another ID from: 10.0, 11.1, 20.3, 6.4. ")
    item2 = float(input("Choose ID from: 10.0, 11.1, 20.3, 6.4. "))

In that case, the variable item2 does not get defined. Here, you attempt to test its value:

#second item
if item2 == 11.1:

On the same code block: you don’t need str(input()): the input() function will return a string object by default.

@Zaid_Shaheed

Format your code block as


```python
```


 so that the code is color highlighted; it’s easier to read that way.

Ok ill try that

I suspect that this is not what you actually want.

Unfortunately the code fromatting is gone, when posting. Thus doing the right format of the code is with you.
Used your program:
Choose ID from: 10.0, 11.1, 20.3, 6.4. 10
How many of these do you need? 2
Do you want to continue? no
Your total cost is: 11.0
After that the following error message occurs
Traceback (most recent call last):
File “C:/Users/Myuser/AppData/Local/Programs/Python/Python310/whatiswrong.py”, line 20, in
if item2 == 11.1:
NameError: name ‘item2’ is not defined. Did you mean: ‘item1’?

item2 is not defined and as such the error shows up., because the answer no does not led to an end of the program and instead the next lines runs ass well and runs into this error

Python does not have a Goto by which you could step to a different line once the lines between should not run.
Define a function at the begin of your program and call that to run the calculation.
Python Functions (w3schools.com)

End the program, when you do not want further steps to run. On that read more about exit and Return within Python Documentation or google on it

if go_ON == “no”:
#These kind of parts need a change in all parts of your program to do the calculation and to end the programm already here.

1 Like
id = [10.0, 11.1, 20.3, 6.4]
price = [5.50, 1.99, 35.00, 26.52]
price1 = 0
price2 = 0
price3 = 0
price4 = 0

#first item
item1 = float(input("Choose ID from: 10.0, 11.1, 20.3, 6.4. "))
if item1 == 10:
  item_number1 = int(input("How many of these do you need? "))
  price1 = price[0] * item_number1
  go_ON = str(input("Do you want to continue? "))
  if go_ON == "no":
    print("Your total cost is:", price1 + price2 + price3 + price4)
  elif go_ON == "yes":
    print("Choose another ID from: 10.0, 11.1, 20.3, 6.4. ")
    item2 = float(input("Choose ID from: 10.0, 11.1, 20.3, 6.4. "))

#second item
if item2 == 11.1:
  item_number2 = int(input("How many of these do you need? "))
  price1 = price[1] * item_number2
  go_ON = str(input("Do you want to continue? "))
  if go_ON == "no":
    print("Your total cost is:", price1 + price2 + price3 + price4)
  elif go_on == "yes":
    print("Choose another ID from: 10.0, 11.1, 20.3, 6.4. ")
    item3 = float(input("Choose ID from: 10.0, 11.1, 20.3, 6.4. "))

#third item
if item3 == 20.3:
  item_number3 = int(input("How many of these do you need? "))
  price1 = price[2] * item_number3
  go_ON = str(input("Do you want to continue? "))
  if go_ON == "no":
    print("Your total cost is:", price1 + price2 + price3 + price4)
  elif go_ON == "yes":
    print("Choose another ID from: 10.0, 11.1, 20.3, 6.4. ")
    item4 = float(input("Choose ID from: 10.0, 11.1, 20.3, 6.4. "))

#fourth item
if item4 == 6.4:
  item_number4 = int(input("How many of these do you need? "))
  price4 = price[3] * item_number4
  print("Your total cost is:", price1 + price2 + price3 + price4)

#error
if item1 != 10.0 or 10:
  print("Error.")
elif item2 != 11.1:
  print("Error.")
elif item3 != 20.3:
  print("Error.")
elif item4 != 6.4:
  print("Error.")

#end
print("Your total is:", price1 + price2 + price3 + price4)

Here is the highlighted code block.

How do I store the list of of numbers: 10.0, 11.1, 20.3, 6.4 in a list[ ] object?
I am kind of a beginner to python and coding in general itself so I don’t know how to.

How do i define item2?

Sorry; I meant to say a “a string object”, like this:

ids = "10.0, 11.1, 20.3, 6.4"

item1 = float(input(f"Choose ID from {ids}: "))

Notice that I’ve used ids as the name. That’s because there is a Python function called id() and it’s best practice not to overwrite any of the built-ins.

To add


You could call a custom function, for each id input, and have said function do some basic error check:

def get_id(message):
    item = False
    id_list = [10, 11.1, 20.3, 6.4]
    id_list.sort()
    ids = ', '.join(str(num) for num in id_list)
    while not item:
        item = input(f"{message} {ids}: ")
        if float(item) not in id_list:
            print("Invalid input.")
            print()
            item = False
        else:
            return float(item)

Now you can get an id input with the likes of item1 = get_id("Choose ID from") and simply change the message string for a new call: item2 = get_id("Choose another ID from")

as a newbie I have entered the above code into

to get the following:

Traceback (most recent call last):
  File "/usr/lib/python3.8/py_compile.py", line 144, in compile
    code = loader.source_to_code(source_bytes, dfile or file,
  File "<frozen importlib._bootstrap_external>", line 846, in source_to_code
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "./prog.py", line 61
    6h
     ^
SyntaxError: invalid syntax

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python3.8/py_compile.py", line 150, in compile
    raise py_exc
py_compile.PyCompileError:   File "./prog.py", line 61
    6h
     ^
SyntaxError: invalid syntax

Which complete code from frist to last line do you exactly refer to? Python trouble shooting is a bit tricky. It will tell you the line by which it recognised an error to be arround. That is not always the line really ever causing the error to occur. When placing any code into the W3Schools Online Code Editor it may show different expressions with different colors. Is anything not colored, which should be colored? Use W3Schools Tryit Editor take care to remember the colors in use for each thing. “Hello world!” is marked in blue. Remove " from “Hello world!” now it shows up in black. Put into place the " again. Now remove the () and run the program. That will give you a hint. Add only the ). That ) will show up in red. Add the ( now both bracktes () show up in black. Try the same by removing the ). Unfortunately, that will remain black rather then be marked in red.

I don’t understand a thing. Please note that I am new to coding so I can’t understand all this stuff

Here is my entire code:

id = [10.0, 11.1, 20.3, 6.4]
price = [5.50, 1.99, 35.00, 26.52]

price1 = 0
price2 = 0
price3 = 0
price4 = 0

#first item
item = float(input("Choose an ID from: 10, 11.1, 20.3, 6.4: "))
if item == 10:
  item_number1 = int(input("How many of these do you need? "))
  price1 = price[0] * item_number1
  go_ON = str(input("Do you want to continue? "))
  if go_ON == "no":
    print("Your total cost is:", price1 + price2 + price3 + price4)
  elif go_ON == "yes":
    print("Choose another ID from: 10.0, 11.1, 20.3, 6.4. ")
    item = float(input("Choose ID from: 10.0, 11.1, 20.3, 6.4. "))

#second item
if item == 11.1:
  item_number2 = int(input("How many of these do you need? "))
  price1 = price[1] * item_number2
  go_ON = str(input("Do you want to continue? "))
  if go_ON == "no":
    print("Your total cost is:", price1 + price2 + price3 + price4)
  elif go_on == "yes":
    print("Choose another ID from: 10.0, 11.1, 20.3, 6.4. ")
    item = float(input("Choose ID from: 10.0, 11.1, 20.3, 6.4. "))

#third item
if item == 20.3:
  item_number3 = int(input("How many of these do you need? "))
  price1 = price[2] * item_number3
  go_ON = str(input("Do you want to continue? "))
  if go_ON == "no":
    print("Your total cost is:", price1 + price2 + price3 + price4)
  elif go_ON == "yes":
    print("Choose another ID from: 10.0, 11.1, 20.3, 6.4. ")
    item = float(input("Choose ID from: 10.0, 11.1, 20.3, 6.4. "))

#fourth item
if item == 6.4:
  item_number4 = int(input("How many of these do you need? "))
  price4 = price[3] * item_number4
  print("Your total cost is:", price1 + price2 + price3 + price4)

#error
if item != 10:
  print("Error.")
elif item != 11.1:
  print("Error.")
elif item != 20.3:
  print("Error.")
elif item != 6.4:
  print("Error.")

#end
print("Your total is:", price1 + price2 + price3 + price4)

And here is the problem occuring:

Choose an ID from: 10, 11.1, 20.3, 6.4: 20.3
How many of these do you need? 10
Do you want to continue? yes
Choose another ID from: 10.0, 11.1, 20.3, 6.4. 
Choose ID from: 10.0, 11.1, 20.3, 6.4. 
Traceback (most recent call last):
  File "main.py", line 41, in <module>
    item = float(input("Choose ID from: 10.0, 11.1, 20.3, 6.4. "))
ValueError: could not convert string to float: ''
îș§ 
KeyboardInterrupt

Ok, let’s have a look. Some comments inline below. Some things will
repeat what others have said.

 id = [10.0, 11.1, 20.3, 6.4]

Rob mentioned that it would be better to name this ids, because the
name id is one of the built in functions. Also, this is a list of
several items, so a plural name is generally better anyway - it will
make expressions using this later read better.

 price = [5.50, 1.99, 35.00, 26.52]

Again, maybe prices instead of price.

 price1 = 0
 price2 = 0
 price3 = 0
 price4 = 0

Looks fine.

 #first item
 item = float(input("Choose an ID from: 10, 11.1, 20.3, 6.4: "))

This seems to recite the items from your list. You could make this track
changes to that list by writing this:

 item = float(input(f"Choose an ID from: {id}: "))

That leading f" makes this a “format string”, which lets you embed the
text {id} in it, and Python will write out your list there.

 if item == 10:
   item_number1 = int(input("How many of these do you need? "))
   price1 = price[0] * item_number1
   go_ON = str(input("Do you want to continue? "))

The result from input() is already a string, you do not need the
str(...) around the input() call. It does no harm, but also it has
no effect.

   if go_ON == "no":
     print("Your total cost is:", price1 + price2 + price3 + price4)
   elif go_ON == "yes":
     print("Choose another ID from: 10.0, 11.1, 20.3, 6.4. ")
     item = float(input("Choose ID from: 10.0, 11.1, 20.3, 6.4. "))

What happens if go_ON is not one of "no" or "yes"?
I’d be inclined to only test for one of these, maybe "yes".

I’m not really sure why you’re asking to go on at all though.
It this a requirement of the exercise you’re attempting?
Normally this kind of question only happens when your running a loop for
input, which might only end when the user enters "no".

I’m going to skip the other 3 item cases, they’re all the same code.

 #error
 if item != 10:
   print("Error.")
 elif item != 11.1:
   print("Error.")
 elif item != 20.3:
   print("Error.")
 elif item != 6.4:
   print("Error.")

A short way to write this is:

 if item not in (10, 11.1, 20.3, 6.4):
   print("Error.")

but usually it is good to say what is wrong, not just “Error.”. This
looks like your id list. You could just go:

 if item not in id:
   print("Error: item should be one of", id)

This tests aginast the list you set up at the start of the programme,
and also shows the user which values are expected in the error message.

Now we get to the thing which makes me wonder about the earlier
structure:

 #end
 print("Your total is:", price1 + price2 + price3 + price4)

This is what I’d expect: after a bunch of inputs and price computations
we print out the total price. This suggests to me that you don’t need
this:

 if go_ON == "no":
   print("Your total cost is:", price1 + price2 + price3 + price4)

in the earlier code. Just do it once down the bottom anyway.

Let’s look at your error:

 And here is the problem occuring:
 ```
 Choose an ID from: 10, 11.1, 20.3, 6.4: 20.3
 How many of these do you need? 10
 Do you want to continue? yes
 Choose another ID from: 10.0, 11.1, 20.3, 6.4.
 Choose ID from: 10.0, 11.1, 20.3, 6.4.
 Traceback (most recent call last):
   File "main.py", line 41, in <module>
     item = float(input("Choose ID from: 10.0, 11.1, 20.3, 6.4. "))
 ValueError: could not convert string to float: ''
 îș§
 KeyboardInterrupt
 ```

It looks to me like you’ve just pressed <Return> after the second
Choose ID from: 10.0, 11.1, 20.3, 6.4. line. That causes input() to
return the empty string '' because you didn’t type a value. And that
empty string cannot be converted to a float, which makes sense.

Python raises an exception in circumstances like this, the ValueError
you see above, and shows you the line of code where it occurred.

If you’re serious about recovering from this you need a little loop
which runs until a valid value is received. Something like this:

 while True:
     item_s = input(f"Choose an ID from: {id} ")
     try:
         item = float(item_s)
     except ValueError as e:
         print("That does not look like a number:", e)
         continue
     break

This runs a loop forever (while True, a condition which never fails)
asking for a number from the user. When the user enters something, it
tries to convert it to a float. If that raises a ValueError, the
user it told the number is no good and the loop does it all again. If
the input text does convert to a float, we break out of the loop.
Then you can compute the price using item.

Then notice that your whole programme seems to test for the expected
item values (10 etc etc). You might do an additional check inside that
loop:

 while True:
     item_s = input(f"Choose an ID from: {id} ")
     try:
         item = float(item_s)
     except ValueError as e:
         print("That does not look like a number:", e)
         continue
     if item not in id:
         print("Unexpected item", item, "I expected one of:", id)
         continue
     break

which would keep looping until the user enters a value from the id
list.

When you break out of the loop you have a valid item, and can compute
the price.

Cheers,
Cameron Simpson cs@cskk.id.au

2 Likes
#See my comments within your code

#first item
item1 = float(input("Choose ID from: 10.0, 11.1, 20.3, 6.4. "))
if item1 == 10:
  item_number1 = int(input("How many of these do you need? "))
  price1 = price[0] * item_number1
  go_ON = str(input("Do you want to continue? "))
  if go_ON == "no":
   # you print out the price. Fine
    print("Your total cost is:", price1 + price2 + price3 + price4)  
    # !!!! But you do not stop the program within the part for if go_ON == "no": !!!!!
    # As such the program will run the further steps after elif part.
    # If you want to prevent the programm to step further here, 
    # then you need to put into place an exit command  within this part for  if go_ON == "no":
    # see https://pythonguides.com/python-exit-command/
   elif go_ON == "yes":
    print("Choose another ID from: 10.0, 11.1, 20.3, 6.4. ")
    item2 = float(input("Choose ID from: 10.0, 11.1, 20.3, 6.4. "))

#For go_ON="no" you will run further with the part below
#second item
#For go_ON="no" you do not have defined item2. As such, the line below will cause an error.
if item2 == 11.1: