I am trying to find whether I should by a diamond or not in this code. I expect to do this:
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")
Error message: We found the following problems with your submission:
We tested your code with cut = "Pear", clarity = "VVS", color = "P", carat = 1.2, budget = 732, preferred_cuts = ["Cushion", "Pear", "Radiant", "Heart", "Emerald"]. We expected your code to print this:
I'll take it!
However, it printed this:
No thanks
Your problem is here:
cost = (100 - colorcost * 0.02)
As stated in the problem, for each rating worse than D, the cost is supposed to decrease by 2% of its original price, which is 100. You're instead multiplying the value of the rating by 0.02.
This can be illustrated by using their example:
#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
As a rule of thumb, when debugging, make sure your code works for given examples, and when problems arise, print out values like print(cost)
to check that it's correct, or, better yet, make tests with assert
.