十进制减法 [英] Decimal Subtraction

查看:84
本文介绍了十进制减法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

求助,我正在写一个基于自动售货机的程序,当用户购买商品时,用户的信用会重新显示给用户,但所有价格都是十进制形式.但是当我购买一件商品时,它会从 1 中减去商品价格,而不是从 1.20 中减去它.有什么想法吗?

Help, Im writing a vending machine based program, and the user's credit is re-displayed to the user when they purchase an item, but all prices are in decimal form. But when I purchase an item, it subtracts the item price from 1, instead of subtracting it from 1.20 for example. Any ideas?

我的代码是:

credit = raw_input("Please input your change, CAREFUL! This Machine only accepts 10p,20p,50p and £1: \n Please enter your change in Pence (Decimal form). e.g' 1.35  ")
list1= ["1", "2", "3", "4", "5"]
price1 = 0.65
price2 = 0.70
price3 = 0.50
price4 = 0.70
price5 = 0.80
while credit == 0:
   credit = float(raw_input("Please input your change: ")))
products = " 1) Kinder Bueno\n 2) Doritos Chilli Heatwave\n 3) Nestle Yorkie Bar\n 4) Coca Cola(Can)\n 5) Volvic Stawberry Water"
print credit
print "The product selection is the following"
print products
selection = raw_input("Please select a product: ")
if selection == list1[0]:
   new_credit = int(float(credit)) - price1 
   print "Your remaining credit is:",new_credit
elif selection == list1[1]:
   new_credit = int(float(credit)) - price2
   print "Your remaining credit is: ",new_credit
elif selection == list1[2]:
   new_credit = int(float(credit)) - price3
   print "Your remaining credit is: ",new_credit
elif selection == list1[3]:
   new_credit = int(float(credit)) - price4
   print "Your remaining credit is: ",new_credit
elif selection == list1[0]:
   new_credit = int(float(credit)) - price5
   print "Your remaining credit is: ",new_credit

它仍然截断并返回以下内容:

It still truncates and returns the following:

Please input your change, CAREFUL! This Machine only accepts 10p,20p,50p and £1: 
 Please enter your change in Pence (Decimal form). e.g' 1.35  1.35
1.35
The product selection is the following
 1) Kinder Bueno
 2) Doritos Chilli Heatwave
 3) Nestle Yorkie Bar
 4) Coca Cola(Can)
 5) Volvic Stawberry Water
Please select a product: 1
Your remaining credit is: 0.35

推荐答案

所以你的代码让我很烦恼.我做了一些改变,让事情变得更加 Pythonic...

So your code bugged me alot. I made some changes to make things more pythonic...

  1. 我没有为您的菜单设置单独的列表,而是将其设为包含产品及其价格的元组列表
  2. 我修复了你的浮动转换

有很多工作要做,例如,捕捉无效输入的错误,在减去价格之前检查可用信用等...

There's alot to be done for example, catching errors on invalid inputs, checking for available credit before subtracting price, etc...

products = [
    ("Kinder Bueno", 0.65),
    ("Doritos Chilli Heatwave", 0.70),
    ("Nestle Yorkie Bar", 0.50),
    ("Coca Cola(Can)", 0.70),
    ("Volvic Stawberry Water", 0.80)
]  
min_price = min(products, key=lambda x:x[1])[1]
credit = float(raw_input("Please input your change, CAREFUL! This Machine only accepts 10p,20p,50p and £1: \n Please enter your change in Pence, at least {0} (Decimal form). e.g' 1.35  ".format(min_price)))

while credit < min_price:
    credit += float(raw_input("Please input more change, you have {0} available, please input at least {1} more: ".format(credit, min_price - credit)))

print "You have {0} credit available.".format(credit)
print "The product selection is the following:"
for i, (product, price) in enumerate(products):
    print "  {0}) {1} {2}".format(i, product, price)

selection = int(raw_input("Please select a product: "))

if 0 >= selection < len(products):
    product, price = products[selection]
    print "You bought {0} for {1}".format(product, price)
    new_credit = credit - price
    print "Your remaining credit is: ", new_credit

通过元组列表,您可以轻松获得最低价格,以确保用户有足够的钱来实际购买:

With the list of tuples, you can easily get a minimum price to ensure the user has enough to actually buy something:

min_price = min(products, key=lambda x:x[1])[1]

获取价格最低的元组并将价格存储在 min_price

which gets the tuple with the lowest price and stores just the price in min_price

这篇关于十进制减法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆