For example, the user will enter four colors (duplicates allowed) where orange = o, blue = b, green = g, red = r. I will assign values to each of these colors.
dictColor = {"o": 4, "b": 2, "g": 7, "r": 5}
colorScore = 0
for i in range(1):
color = input("Enter 4 characters of colors: ")
if "o" in color:
colorScore += dictColor["o"]
if "b" in color:
colorScore += dictColor["b"]
if "g" in color:
colorScore += dictColor["g"]
if "r" in color:
colorScore += dictColor["r"]
print("The color score is",colorScore)
If the user were to enter in rbgo, the program works fine and outputs a value of 18. However, if the user entered one duplicate, such as rrgo, the program outputs 16, which is not the correct output. How would I make my program able to recognize these duplicates?
for i in range(1):?for i in range(1):is a loop that iterates once.range(1)produces a sequence with only one element in it, the integer0. Since you don't useiin the loop, and the range is hard-coded, the conclusion is that the loop is meaningless and busywork, the body of the loop is executed just once so theforloop can safely be removed and the whole body un-indented to match the rest of the code.