2017-10-06 5 views
0

問題は次のとおりです。 事業者は、購入単位数と単価に基づいて割引を適用します。ユーザーにユニット数と単価を尋ね、合計価格、適用された割引、合計割引価格を計算して出力するアプリケーションを作成します。単位数が0以下の場合、または単位当りの価格が€0以下の場合は、適切なエラーメッセージを表示してください。このコードでの範囲と浮動小数点数のインターミキシング

ここに私のコードです:

units_brought = float(input("Enter the number of units brought ")) 
unit_price = float(input("Enter the unit price ")) 

total_price = units_brought * unit_price 

discount_applied = float(0) 

total_discounted_price = total_price - discount_applied 

if units_brought in range(1, 49) and unit_price in range(0.01, 10) : 
    print("The total price is ",total_price,"\nThe discount applied is",discount_applied,"\nThe total discounted price is ",total_discounted_price) 

    if unit_price in range(10.01, 100) : 
     discount_applied = total_price * 0.05 
     print("The total price is ", total_price, "\nThe discount applied is", discount_applied,"\nThe total discounted price is ", total_discounted_price) 

    elif unit_price >= 100.1: 
      discount_applied= total_price * 0.08 
      print("The total price is ", total_price, "\nThe discount applied is", discount_applied,"\nThe total discounted price is ", total_discounted_price) 

elif units_brought in range(50, 99) and unit_price in range(0.01, 10) : 
    discount_applied = total_price * 0.12 
    print("The total price is ", total_price, "\nThe discount applied is", discount_applied, 
      "\nThe total discounted price is ", total_discounted_price) 

    if unit_price in range(10.01, 100) : 
     discount_applied = total_price * 0.18 
     print("The total price is ", total_price, "\nThe discount applied is", discount_applied, 
       "\nThe total discounted price is ", total_discounted_price) 

    elif unit_price >= 100.01: 
      discount_applied = total_price * 0.22 
      print("The total price is ", total_price, "\nThe discount applied is", discount_applied, 
        "\nThe total discounted price is ", total_discounted_price) 

elif units_brought >= 100 and unit_price in range(0.01, 10) : 
    discount_applied = total_price * 0.21 
    print("The total price is ", total_price, "\nThe discount applied is", discount_applied, 
      "\nThe total discounted price is ", total_discounted_price) 

    if unit_price in range(10.01, 100): 
     discount_applied = total_price * 0.32 
     print("The total price is ", total_price, "\nThe discount applied is", discount_applied, 
       "\nThe total discounted price is ", total_discounted_price) 

    elif unit_price >= 100.01: 
     discount_applied = total_price * 0.43 
     print("The total price is ", total_price, "\nThe discount applied is", discount_applied, 
       "\nThe total discounted price is ", total_discounted_price) 

elif units_brought <= 0: 
    print("The number of units brought can't be zero or less") 

elif unit_price <= 0: 
    print("The unit price can't be zero or less") 

これは、それが実行されるときに何が起こるかです:

Enter the number of units brought 5 
Enter the unit price 4 
Traceback (most recent call last): 
    File "C:/Users/User/Documents/My code/business_price_&_discounts.py", line 19, in <module> 
    if units_brought in range(1, 49) and unit_price in range(0.01, 10) : 
TypeError: 'float' object cannot be interpreted as an integer 

Process finished with exit code 1 

をので、問題はながら山車を持つことができないif文で範囲機能のようです同時に小数点を必要とする私の価値、私の質問は、ここに範囲関数の代替ですか?フォームで

答えて

2

あなた比較

if unit_price in range(10.01, 100): 

rangeのみ、整数の引数を取りますので、動作しません。代わりに、あなたは

if 10.01 <= unit_price < 100: 

を書くために、連鎖比較を使用することができます。またこのように、私はその比較で< 100を使用し、範囲内のtoパラメータが排他であることに注意してください。しかし、あなたの場合は、の>= 100.1を次のように指定して、<= 100をチェックすることをお勧めします。またによる浮動小数点演算には、このようにあなたが以下のelifif<= 100> 100をテストしたいかもしれない、あなただけの0.1の増分を使用している場合でも、値が100.0100.1の間にある可能性があることに注意してください。

関連する問題