我正在尝试在此代码中查找是否应使用菱形。我希望这样做:
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”,净度=“ VVS”,color =“ P”,克拉= 1.2,预算= 732,preferred_cuts = [“ Cushion”,“ Pear”,“ Radiant”,“心”,“翡翠”]。我们希望您的代码能够打印出此内容:
我接受!
但是,它打印了此内容:
不,谢谢
答案 0 :(得分:1)
您的问题在这里:
cost = (100 - colorcost * 0.02)
如问题中所述,对于每个差于D的评分,应该使成本降低其原始价格(即100)的2%。您应该将评分的值乘以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)
之类的值以检查其是否正确,或者最好使用{ {1}}。