简体   繁体   中英

How to print the "remaining amount" inside the input in python

I'm struggling to print the remaining amount inside the input

I tried to remove the money the users enter from the cost choice but it doesn't work

prod = ["Coffee", "Coffee with milk", "Chocolate", "Chocolate with milk"]
cost = [1.5, 1.8, 2.1, 2.4]
m = [0.1, 0.2, 0.5, 1, 2, 5, 10]

item_number = len(prod)

print("\nYour choices:")

for number in range(0, item_number, 1):
    print(number + 1,
    prod [number],
    '{0:.2f}€'.format(cost[number]))

print ("0 Exit")

choice = int(input("Please enter a choice between (1-4) or hit 0 for exit: ")) -1

if choice < item_number and choice >= 0:
    print("Please enter", "{0:.2f}€".format(cost[choice]), 'in total')
else:
        print("Exiting the program")
        exit(0)

money = float(input("Please fill the remaining amount ; "))

while money < cost[choice]:

    money += float(input("You still have to enter ", cost[choice] - money))

change = money - cost[choice]

print("Your change {0:.2f}€".format(change))

Your problem is in this line.

money += float(input("You still have to enter ", cost[choice] - money))

which should be

money += float(input("You still have to enter " + str(cost[choice] - money)))

Make the calculation first (inside parentheses) then, convert it to a string to concentrate with the input string.

Does this Help?

prod = ["Coffee", "Coffee with milk", "Chocolate", "Chocolate with milk"]
cost = [1.5, 1.8, 2.1, 2.4]
m = [0.1, 0.2, 0.5, 1, 2, 5, 10]

item_number = len(prod)

print("\nYour choices:")

for number in range(0, item_number, 1):
    print(number + 1,
    prod [number],
    '{0:.2f}€'.format(cost[number]))

print ("0 Exit")

choice = int(input("Please enter a choice between (1-4) or hit 0 for exit: ")) 
-1

if choice < item_number and choice >= 0:
    print("Please enter", "{0:.2f}€".format(cost[choice]), 'in total')
else:
        print("Exiting the program")
        exit(0)

money = float(input("Please fill the remaining amount ; "))

while money < cost[choice]:
    money += float(input("You still have to enter "+str("{:.2f}".format(cost[choice] - money))))

change = money - cost[choice]

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