我对edx CS1301xII问题3.2.8的回答有问题
My answer to edx CS1301xII question 3.2.8 has a problem
我正在尝试确定我是否应该在此代码中使用钻石。我希望这样做:
- 不买钻石是他们喜欢的切工
- 买不起就不要买
这是我的代码:
cut = "Emerald"
clarity = "VS"
color = "I"
carat = 3.3
budget = 1271
preferred_cuts = ["Emerald", "Cushion", "Princess", "Oval"]
#You may modify the lines of code above, but don't move them!
#When you Submit your code, we'll change these lines to
#assign different values to the variables.
#Diamonds are typically evaluated according to four aspects:
# - Cut: The way the diamond is cut
# - Clarity: How clear or flawless the diamond is, rated
# as F (the best), IF, VVS, VS, SI, or I (the worst)
# - Color: How colorless the diamond is, rated from "D" (the
# best) to "Z" (the worst)
# - Carat: How large the diamond is, weighed in carats
#
#Cut is usually a matter of personal preference. Clarity,
#color, and carat are matters of value: the clearer, more
#colorless, and larger a diamond is, the greater its value.
#
#Imagine you're shopping for a diamond. You have your
#preferred cuts, and you have a budget in mind. You're shown
#a diamond whose characteristics are represented by the cut,
#color, clarity, and carat variables above. You'll buy the
#diamond if its cost is less than your budget, and if its
#cut is one of your preferred cuts.
#
#At this store, every diamond has a base cost of 100.
#
#For every color rating worse than "D", the cost decreases by
#2%. An "F" color diamond would be worth 0.96 * the diamond
#cost otherwise because "F" is two colors worse than "D".
#
#A diamond's value is doubled for every level of clarity above
#I. A "VVS"-clarity diamond is worth 8 * the diamond cost
#otherwise because "VVS" is three levels higher than I, and
#doubling its value three times raises its value by 8x total.
#
#After finding its price based on color and clarity, its price
#is multiplied by its weight in carats.
#
#Your program should print "I'll take it!" if you will buy the
#diamond, "No thanks" if you will not. To purchase it, its price
#must be less than your budget and its cut must be one of your
#preferred cuts.
#
#HINT: You can find the number of characters between two
#characters by using the ord() function. ord("E") - ord("D")
#is 1; ord("Z") - ord("D") is 22.
#
#HINT 2: We haven't covered lists, but we did cover how to
#see if an item is present in a list using the 'in' operator
#in chapter 2.3.
#Add your code here!
colorcost = ord(color) - ord("D")
cost = (100 - colorcost * 0.02)
if clarity == "SI":
cost *= 2
elif clarity == "VS":
cost *= 4
elif clarity == "VVS":
cost *= 8
elif clarity == "IF":
cost *= 16
elif clarity == "F":
cost *= 32
cost *= carat
if cut not in preferred_cuts:
print("No thanks")
elif ((budget - cost) >= 0):
print("I'll take it!")
else:
print("No thanks")
错误信息:
我们发现您的提交存在以下问题:
我们用 cut = "Pear", clarity = "VVS", color = "P", carat = 1.2, budget = 732, preferred_cuts = ["Cushion"、"Pear"、"Radiant"、"Heart"、"Emerald"]。我们希望您的代码打印此内容:
我要了!
但是,它打印了这个:
不用谢
你的问题在这里:
cost = (100 - colorcost * 0.02)
如问题中所述,对于每一个比 D 差的评级,成本应该减少其原始价格的 2%,即 100。您要将评级的值乘以 0.02。
这可以用他们的例子来说明:
#For every color rating worse than "D", the cost decreases by
#2%. An "F" color diamond would be worth 0.96 * the diamond
#cost otherwise because "F" is two colors worse than "D".
color = "F"
colorcost = ord(color) - ord("D")
cost = 100 - colorcost*0.02 # <-- this equals 96.96
correct_cost = 0.96*100 # <-- correct cost should be 96
assert cost == correct_cost # this test fails
根据经验,在调试时,请确保您的代码适用于给定的示例,并且当出现问题时,打印出 print(cost)
之类的值以检查其是否正确,或者,更好的是,进行测试assert
.
我正在尝试确定我是否应该在此代码中使用钻石。我希望这样做:
- 不买钻石是他们喜欢的切工
- 买不起就不要买 这是我的代码:
cut = "Emerald"
clarity = "VS"
color = "I"
carat = 3.3
budget = 1271
preferred_cuts = ["Emerald", "Cushion", "Princess", "Oval"]
#You may modify the lines of code above, but don't move them!
#When you Submit your code, we'll change these lines to
#assign different values to the variables.
#Diamonds are typically evaluated according to four aspects:
# - Cut: The way the diamond is cut
# - Clarity: How clear or flawless the diamond is, rated
# as F (the best), IF, VVS, VS, SI, or I (the worst)
# - Color: How colorless the diamond is, rated from "D" (the
# best) to "Z" (the worst)
# - Carat: How large the diamond is, weighed in carats
#
#Cut is usually a matter of personal preference. Clarity,
#color, and carat are matters of value: the clearer, more
#colorless, and larger a diamond is, the greater its value.
#
#Imagine you're shopping for a diamond. You have your
#preferred cuts, and you have a budget in mind. You're shown
#a diamond whose characteristics are represented by the cut,
#color, clarity, and carat variables above. You'll buy the
#diamond if its cost is less than your budget, and if its
#cut is one of your preferred cuts.
#
#At this store, every diamond has a base cost of 100.
#
#For every color rating worse than "D", the cost decreases by
#2%. An "F" color diamond would be worth 0.96 * the diamond
#cost otherwise because "F" is two colors worse than "D".
#
#A diamond's value is doubled for every level of clarity above
#I. A "VVS"-clarity diamond is worth 8 * the diamond cost
#otherwise because "VVS" is three levels higher than I, and
#doubling its value three times raises its value by 8x total.
#
#After finding its price based on color and clarity, its price
#is multiplied by its weight in carats.
#
#Your program should print "I'll take it!" if you will buy the
#diamond, "No thanks" if you will not. To purchase it, its price
#must be less than your budget and its cut must be one of your
#preferred cuts.
#
#HINT: You can find the number of characters between two
#characters by using the ord() function. ord("E") - ord("D")
#is 1; ord("Z") - ord("D") is 22.
#
#HINT 2: We haven't covered lists, but we did cover how to
#see if an item is present in a list using the 'in' operator
#in chapter 2.3.
#Add your code here!
colorcost = ord(color) - ord("D")
cost = (100 - colorcost * 0.02)
if clarity == "SI":
cost *= 2
elif clarity == "VS":
cost *= 4
elif clarity == "VVS":
cost *= 8
elif clarity == "IF":
cost *= 16
elif clarity == "F":
cost *= 32
cost *= carat
if cut not in preferred_cuts:
print("No thanks")
elif ((budget - cost) >= 0):
print("I'll take it!")
else:
print("No thanks")
错误信息: 我们发现您的提交存在以下问题:
我们用 cut = "Pear", clarity = "VVS", color = "P", carat = 1.2, budget = 732, preferred_cuts = ["Cushion"、"Pear"、"Radiant"、"Heart"、"Emerald"]。我们希望您的代码打印此内容:
我要了!
但是,它打印了这个:
不用谢
你的问题在这里:
cost = (100 - colorcost * 0.02)
如问题中所述,对于每一个比 D 差的评级,成本应该减少其原始价格的 2%,即 100。您要将评级的值乘以 0.02。
这可以用他们的例子来说明:
#For every color rating worse than "D", the cost decreases by
#2%. An "F" color diamond would be worth 0.96 * the diamond
#cost otherwise because "F" is two colors worse than "D".
color = "F"
colorcost = ord(color) - ord("D")
cost = 100 - colorcost*0.02 # <-- this equals 96.96
correct_cost = 0.96*100 # <-- correct cost should be 96
assert cost == correct_cost # this test fails
根据经验,在调试时,请确保您的代码适用于给定的示例,并且当出现问题时,打印出 print(cost)
之类的值以检查其是否正确,或者,更好的是,进行测试assert
.