-1

The project requires us to increase by 0.25 for the total cost for every ketchup packet users would want. The main issue I'm having is trying to get it to increment the cost by 0.25 for every packet. For example for 4 packets, 1.00 would be added to the total cost. In addition, could help me out with also making it to where if they enter anything besides an integer, it will stop the code.

My code for this part of it is the following.

print("How many ketchup packets would you like? The Cost is 0.25 every packet.")

ketchupPack = input("How many packets do you want? Please enter a positive integer if possible.")
ketchupPack=int(ketchupPack)

if(ketchupPack>=1):
print("Thanks for entering the amount of Ketchup you would like!")
totalCost + 0.25
print("The total cost of your meal so far is $" + str(round(totalCost,2)))

else:

print(totalCost)
pakpe
  • 5,391
  • 2
  • 8
  • 23

1 Answers1

1

There are several problems with your code.

First of all, your indentation is wrong; the code in your if and else blocks must be indented. A SyntaxError will be thrown if it is not.

Next, totalCost + 0.25 doesn't do what you think; it's an expression, not a statement, and it doesn't change the variable's value (see here for more information). To actually change its value, you would need to use totalCost = totalCost + 0.25, which can also be abbreviated to totalCost += 0.25.

With that change in place, you're still only adding 0.25 to totalCost no matter how many ketchup packets are bought. You need to multiply 0.25 by the number of packets to get the correct value.

As for handling non-integer inputs for ketchupPack, you can use a try-except statement. These statements allow you to try running code and, in the case of an exception being thrown, run different code to account for the exception.

With these changes in place, your code should look like this:

print("How many ketchup packets would you like? The Cost is 0.25 every packet.")

ketchupPack = input("How many packets do you want? Please enter a positive integer if possible.")
try:
    ketchupPack = int(ketchupPack)
except ValueError:
    print("Invalid input: defaulting to 1000000 ketchup packs.")
    ketchupPack = 1000000

totalCost = 0

if(ketchupPack>=1):
    print("Thanks for entering the amount of Ketchup you would like!")
    totalCost += 0.25 * ketchupPack
    print("The total cost of your meal so far is $" + str(round(totalCost,2)))
else:
    print(totalCost)

Now, if the conversion to an integer fails, it will print "Invalid input: defaulting to 1000000 ketchup packs." and set ketchupPacks to 1000000 because, well, everyone loves ketchup.

Another option is to create a loop and continue asking for input until the input is a valid integer value, then break from the loop, like so:

print("How many ketchup packets would you like? The Cost is 0.25 every packet.")

while True:
    try:
        ketchupPack = input("How many packets do you want? Please enter a positive integer if possible.")
        ketchupPack = int(ketchupPack)
        break
    except ValueError:
        print("Invalid input: please enter a positive integer value.")

This will continuously take user input and attempt to convert it to an integer. If the conversion is successful, the program will exit the loop. If not, the except statement will be run, informing the user that their input is invalid. The loop will continue iterating until a valid value is entered.

Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
  • nice explanation – chess_lover_6 Feb 22 '21 at 19:11
  • Ok, but another error I have is this I use the try and except on the program once more. Fpr some reason it wont work? Please help. print("Would you like a drink?") print("Enter 1 if Yes.") print("Enter 2 if No.") bevOption = input("Please enter 1 or 2 for beverage.") try: bevOption = int(bevOption) except ValueError: print("Invalid Option. No Beverage for you.") bevOption==2 totalCost = totalCost if(bevOption==1): print("Thanks for selecting a small, Price is 1.00") totalCost += 1.00 print("The total is $" + str(round(totalCost,2))) – Dippy_Scff Feb 22 '21 at 20:05
  • @Dippy_Scff code blocks don't work in comments, but if I understand the indentation correctly, the problem lies with a lack of declaration for `totalCost`; you reference it in several places (e.g. in the last `if` statement you use `totalCost += 1.00`) before having actually defined it, resulting in a NameError. Adding a `totalCost = 0` declaration near the beginning of the program should fix the issue. – Pika Supports Ukraine Feb 22 '21 at 20:42
  • @PikalèSorcereroftheWhales I have a totalCost==0 declaration at the beginning of the program. The issue is that with my code, I have to make sure everything works first, which I do, but the main issue is the fact that I have to make sure my teacher can´t put anything in that will break it besides what is given, like, I can´t let 4, or a word such as banana break the code but the words or integers can be anything. – Dippy_Scff Feb 23 '21 at 15:17
  • @Dippy_Scff I think you're getting `=` and `==` confused. One equals sign means to assign a variable's value; two mean to compare it to something. `totalCost==0` is not actually changing `totalCost`'s value, it's just checking if it's equal to 0. It should be `totalCost=0`. – Pika Supports Ukraine Feb 23 '21 at 15:44
  • @PikalèSorcereroftheWhales Ill post another question with all of my code if you would like to see all of it. Cause I'm having trouble with multiple sections, it would definitely help if you could. The main issue I'm having is when I have try and except for the beverage option, if I enter something besides the options given, it just says, that I chose to have a drink? I don't know why. I will post the code so you can see. Or if you have another way I can contact you that'd be great such as possibly doing it through private messages on here. – Dippy_Scff Feb 23 '21 at 16:07
  • @Dippy_Scff Stack Overflow doesn't have a private messaging feature. Feel free to post a new question; I can't guarantee I'll be able to answer it at the moment, but if not me someone else likely will. – Pika Supports Ukraine Feb 23 '21 at 17:19
  • @PikalèSorcereroftheWhales I figured it out. The whole try and except thing doesn't work very well unless you are just trying to avoid an actual word being put in. I used a forever loop instead with a list so it will repeat the question unless certain conditions are met. – Dippy_Scff Feb 23 '21 at 20:06
  • @Dippy_Scff ah, I think I misunderstood what you were asking, but congrats on figuring it out! – Pika Supports Ukraine Feb 23 '21 at 20:13
  • @PikalèSorcereroftheWhales I posted a new question. Could you take a look at it? My code works, it just had some new problems arise. – Dippy_Scff Feb 25 '21 at 17:34
  • @PikalèSorcereroftheWhales here's the link to my code if you want a closer work at how my program is functioning. The new question explains all the errors that I have been experiencing now. – Dippy_Scff Feb 25 '21 at 17:35
  • @Dippy_Scff thank you for letting me know, I will take a look when I get the chance :) – Pika Supports Ukraine Feb 25 '21 at 17:49