我对edx CS1301xII问题3.2.8的回答有问题

2024-05-16 23:49:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在试图找出我是否应该在这个代码中使用菱形。我希望这样做:

  • 不要买他们喜欢的钻石
  • 如果我买不起,就不要买 这是我的代码:
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”、clearity=“VVS”、color=“p”、克拉=1.2、预算=732、首选的_-cuts=[“缓冲”、“梨”、“辐射”、“心”、“翡翠”]测试了您的代码。我们希望您的代码打印以下内容:

我要了

然而,它打印了以下内容:

不,谢谢


Tags: andoftheinyourifisclarity
1条回答
网友
1楼 · 发布于 2024-05-16 23:49:17

你的问题是:

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进行测试

相关问题 更多 >