【问题标题】:Help with a python code帮助一个python代码
【发布时间】:2011-06-27 14:05:29
【问题描述】:

我正在阅读这本名为“Python for software design”的 Python 书籍,其中包含以下练习:

假设一本书的封面价格是 24.95 美元,但书店获得了 40% 的折扣。 第一份运费为 3 美元,每增加一份运费为 75 美分。什么 是 60 份的总批发成本

好的,我有以下代码:

bookPrice = 24.95
discount = .60
shippingPriceRest = .75
shippingPriceFirst = 3.00
totalUnits = 60

bookDiscountAmount = bookPrice * discount * totalUnits
shipping = shippingPriceRest * 59 + shippingPriceFirst

result = bookDiscountAmount + shipping


print 'The total price for 60 books including shipping and discount is: '
print 'Total price of the books is: ' + str(bookDiscountAmount)
print 'Total Shipping is: ' + str(shipping)
print 'The Total price is: ' + str(result)

我得到以下结果:

  • 60本书的总价包括运费和折扣是:
  • 图书总价:898.2
  • 总运费为:47.25
  • 总价为:945.45

  • 我的问题是:

    • 这是正确的吗?
    • 我怎样才能使这段代码更好?

【问题讨论】:

  • 更多的是基础数学而不是 Python...
  • 是的,我正在尝试学习语法、变量以及如何处理它们。
  • 一个建议:不要在金钱上使用浮点数。以美分记录所有价格。用美分进行所有计算。输出时转换为美元。
  • 是的,我读到浮点数不是很精确

标签: python


【解决方案1】:

只有三件事需要改变:

1) 您重复了书籍的数量:60 和 59 都出现在代码中。那里不应该有 59。

2) 像这样打印你的结果:print 'The total price is: %.2f' % result

3) 通常的 Python 约定是命名 variables_like_this,notLikeThis。

【讨论】:

  • 那么一个好方法是执行以下操作? shipping = shippingPriceRest * totalUnits - 1 + shippingPriceFirst
  • 如果你尝试这样做,你会发现你需要包含一些括号来评估你的意思。
【解决方案2】:

我建议的唯一改进是使用format-函数而不是字符串连接:

print """The total price for {0:d} books including shipping and discount is: 
         Total price of the books is: {1:7.2f} 
         Total Shipping is:           {2:7.2f} 
         The Total price is:          {3:7.2f}""".format(totalUnits, bookDiscountAmount
                                                         shipping, result)

这使得所有数字都很好地对齐并且格式相同(小数点后两位数,总精度为 7)。

编辑:当然,正如另一位指出的那样,不要在那里硬编码59。

【讨论】:

    【解决方案3】:

    在我看来是对的。我会避免硬编码 59。相反,检查总数是否大于 1,并酌情划分。

    另外,这是次要的,但bookDiscountAmount 应该是bookDiscountedAmount(折扣是他们节省的金额,而不是他们支付的金额)。其他人已经指出了如何改进字符串打印。

    【讨论】:

    • 是的,对不起,英语不是我的母语,谢谢提醒
    【解决方案4】:

    我是这样做的,但我仍然相信使用上述功能会更好

    price = 24.95
    discount = price * (40/100)
    d_price = price - discount
    shipping = 3 + (0.75 * (60 - 1))
    wholesale = d_price * 60 + shipping
    

    【讨论】:

      【解决方案5】:

      请试试这个:

      bookCost = 24.95
      numBooks = 60.0
      
      def cost(numBooks):
          bulkBookCost = ((bookCost * 0.60) * numBooks)
          shippingCost = (3.0 + (0.75 * (numBooks - 1)))
          totalCost = bulkBookCost + shippingCost
          print 'The total cost is: $', totalCost
      
      cost(numBooks)
      

      【讨论】:

        【解决方案6】:
        '''Suppose the cover price of a book is $24.95, but bookstores get a 40%
        discount. Shipping costs $3 for the first copy and 75 cents for each
        additional copy. What is the total wholesale cost for
        60 copies?
        '''
        import math
        
        book_price = float(24.95)
        
        discount = float(book_price * 40 /100)
        
        Book_Price= float(book_price - discount)
        print ('Book Price is without shipping charges = ' , Book_Price)
        
        shipping = 3.0
        
        Total_Price = float(Book_Price + shipping)
        
        print ('The book 1st price is =' ,Total_Price)
        
        Quantity = int(input('Enter the Number of Copies = '))
        
        if Quantity > 1:
        
           Price = float(Book_Price * Quantity)
           Additional_copy_price = 0.75
           Q = float(Quantity * Additional_copy_price) 
           Final_Price = float(Price + Q + 3 - .75)
        
           print (' Final price for more than one copy is  = ' , Final_Price)
        
        else:
           print (Total_Price)
        

        【讨论】:

          【解决方案7】:
          cov_price = 24.95                            #cost of one book
          discount = 0.4
          per_one = (cov_price-(cov_price*discount))   #cost of one book on discount
          ship_cost = 3                                #shipment on first book
          other_ship = 0.75                            #shipment on other books
          
          purchased = input("Enter number of books purchased : ")
          purchased = int(purchased)
          
          if(purchased):
             cost = ((other_ship*(purchased-1)+(per_one*(purchased-1))) + (per_one+3))
             print("The cost of books purchased including discount = ",cost)
             print("Price per one is =",per_one)
          

          我是 python 的初学者,这对我来说很好用

          【讨论】:

            【解决方案8】:
                bc = 24.95    #book cost real price
            dis = 0.40    # discount percent
            bcd = bc*dis  # discount amount
            bp = bc - bcd # book price
            
            scf = 3       # shipping cost for the first book
            scr = 0.75    # shipping cost for the rest books
            
            cost1 = bp*scf
            cost2 = bp*scr*59
            TotalCost = cost1 + cost2
            print(TotalCost)
            

            【讨论】:

            • 您可能应该描述代码比以前更好的地方,以及原始问题是否正确。
            【解决方案9】:

            这是我对这个问题的基本解决方案

            price = 24.95 - (24.95)*4/10
            order = 60
            shipping = 3 + 0.75*(order-1)
            print('The total price for 60 books including shipping and discount is %.2f$' % (order*price + shipping))
            

            【讨论】:

              【解决方案10】:
              n = int(input('Enter the number of copies : '))
              
              Book_store_price_of_one_copy = (60*24.95)/100
              
              Shipping_cost_for_copy = (3+((n-1)*0.75))
              
              whole_sale = (n*Book_store_price_of_one_copy)+Shipping_cost_for_copy
              
              print(f'Whole sale of {n}  copies is {whole_sale}')
              

              【讨论】:

              • 嗨,欢迎来到 Stackoverflow!下一次,请尝试解释代码的每个部分的作用,或者至少解释它是如何回答问题的。它使那些再次偶然发现您的代码的人更容易。谢谢!
              【解决方案11】:
              #######################################################################################
              #Your code, commented
              #######################################################################################
              
              bookPrice = 24.95
              discount = .60
              shippingPriceRest = .75
              shippingPriceFirst = 3.00
              totalUnits = 60
              
              bookDiscountAmount = bookPrice * discount * totalUnits # Poor variable name choice. This is not the book discount amount, it's the cost of all books without shipping
              shipping = shippingPriceRest * 59 + shippingPriceFirst # You should really use your variables as much as possible. Instead of 59, use totalUnits - 1
              
              result = bookDiscountAmount + shipping
              
              
              print 'The total price for 60 books including shipping and discount is: '
              print 'Total price of the books is: ' + str(bookDiscountAmount)
              print 'Total Shipping is: ' + str(shipping)
              print 'The Total price is: ' + str(result)
              
              #######################################################################################
              #An example of your code, cleaned up
              #######################################################################################
              
              bookPrice = 24.95
              discount = .60
              shippingPriceRest = .75
              shippingPriceFirst = 3.00
              totalUnits = 60
              
              totalCostBeforeShipping = (bookPrice * discount) * totalUnits
              shipping = (shippingPriceRest * (totalUnits-1)) + shippingPriceFirst
              
              result = totalCostBeforeShipping + shipping
              
              print 'The total price for 60 books including shipping and discount is: '
              print 'Total price of the books is: ' + str(totalCostBeforeShipping)
              print 'Total Shipping is: ' + str(shipping)
              print 'The Total price is: ' + str(result)
              
              #######################################################################################
              #An example of another method, using a loop
              #######################################################################################
              
              bookPrice = 24.95
              discount = 40 # %
              bookPriceWithDiscount = bookPrice * ((100.0-discount)/100.0)
              firstBookShippingPrice = 3
              otherBookShippingPrice = .75
              totalBooks = 60
              
              totalCost = 0
              
              for book in range(totalBooks):
                  if book == 0:
                      totalCost += firstBookShippingPrice
                  else:
                      totalCost += otherBookShippingPrice
              
                  totalCost += bookPriceWithDiscount
              
              shipping = firstBookShippingPrice + (otherBookShippingPrice * totalBooks)
              
              print 'The total price for 60 books including shipping and discount is:'
              print 'Total price per book is: %d'%(bookPriceWithDiscount)
              print 'Total shipping is: %d'%(shipping)
              print 'The total price is: %d'%(result)
              

              【讨论】:

              • %d ?你试过这个代码吗?一个循环增加运费?为什么??
              猜你喜欢
              • 1970-01-01
              • 2023-03-24
              • 2014-12-23
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2023-04-01
              • 2012-03-22
              • 1970-01-01
              相关资源
              最近更新 更多