简体   繁体   中英

How to increase totalCost based off the integer value user enters?

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM