0

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?

5
  • 6
    What is the point of for i in range(1):? Commented Jun 21, 2018 at 16:56
  • @ScottHunter I'm not too sure myself. I am still fairly new to Python, so I'm just testing out how codes work. I will delete that afterwards. Commented Jun 21, 2018 at 16:57
  • It didn't write itself; what did you think it was accomplishing? Commented Jun 21, 2018 at 17:01
  • 3
    for i in range(1): is a loop that iterates once. range(1) produces a sequence with only one element in it, the integer 0. Since you don't use i in 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 the for loop can safely be removed and the whole body un-indented to match the rest of the code. Commented Jun 21, 2018 at 17:01
  • Don't fall into the trap of programming by coincidence; really think about what the code you're writing means. Commented Jun 21, 2018 at 20:27

4 Answers 4

4

You are only testing for each character once. Loop over the input string, and use each character as a key for your dictionary:

dictColor = {"o": 4, "b": 2, "g": 7, "r": 5}
color = input("Enter 4 characters of colors: ")

colorScore = 0
for character in color:
    if character in dictColor:
        colorScore += dictColor[character]

or, using the sum() function, and dict.get() to return a default colour score of 0 for any unrecognised character:

dictColor = {"o": 4, "b": 2, "g": 7, "r": 5}
color = input("Enter 4 characters of colors: ")
colorScore = sum(dictColor.get(c, 0) for c in color)

The above passes a generator expression to the sum() function, which will loop over each result of the expression. So for each c in the string color (each character), the dictColor.get(c, 0) expression fetches the value for the given key or 0 if the key is missing, and sum() adds up all those results.

Sign up to request clarification or add additional context in comments.

Comments

0

For each character in color, look up its score in the dictionary, and add up those values.

Comments

0

Probably the fastest and more readable solution is with list comprehension:

dictColor = {"o": 4, "b": 2, "g": 7, "r": 5}
color = input("Enter 4 characters of colors: ")
print(sum((dictColor[c] for c in color)))

5 Comments

There is no need to build a list first. Drop the [ and ] and make it a generator expression.
You didn't use dict.get nor an if statement to check if the c is inside the dictColor. That may raise an exception from a wrong input.
@EnderLook that was not a request and you do not know how the programmer wants to manage the error. Which default value is correct? Is it even ok to default a value or is better to have a try/except clause?
But in the OP example he already managed that. He used 4 if statements, one for each letter. So we already know that OP don't want to sum wrong letters (in that case, wrong letters are the same as 0)
that's the difference from a feature and a bug: the OP doesn't want that, his desired behaviour is to obtain the sum of the values corresponding to some given colors. I'm pretty sure that returning 14 to rg%&/b is wrong, raising an error is right. BTW I'm not sure that we're producing useful information right now.
0

Because you didn't iterate over each character from the string input:

Try this while loop:

while True:
    color = input("Enter 4 characters of colors: ")
    for letter in color: # This iterate over each letter of the input
        if letter in dictColor: # If the letter is on your color's dictionay
            colorScore = colorScore + dictColor[letter]
    print("The color score is",colorScore)

Or if you look for an smaller code:

while True:
    color = input("Enter 4 characters of colors: ")
    for letter in color:
            colorScore += dictColor.get(letter, 0)
    print("The color score is",colorScore)

Or even smaller:

dictColor = {"o": 4, "b": 2, "g": 7, "r": 5}
while True:
    color = input("Enter 4 characters of colors: ")
    colorScore = sum(dictColor.get(letter, 0) for letter in color)
    print("The color score is", colorScore)

sum(iterable[, start]): This function iterate over all the iterable items (e.g: items from a list, tuple, set) and sum their values.

dictionary.get(key[, default]): This function return the value of a key in a given dictionary. In the case it doesn't exist that value return a default one (instead of raise an exception).

generator expression: The sum function will iterate over the result of the generator expression. So for each letter in the string input, the dictColor.get(letter, 0) expression will look for the value of that key in the dictColor.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.