4.2 Exercise 2

From the exercises repo, merge branches exercise2/branchA and exercise2/branchB.

Conflict

Listing 4.3:Exercise 2 - conflict
1#!/usr/bin/python 
2 
3import sys 
4 
5colors = {"black": "black mirror", 
6          "white":  "white noise", 
7          "blue": "blue sky"} 
8 
9def getPhrase(color): 
10<<<<<<< HEAD 
11    phrase = colors[color.lower()] 
12======= 
13    if color not in colors: 
14        sys.stderr.write("Got no phrase for color %s\n" % color) 
15        sys.exit(1) 
16    phrase = colors[color] 
17>>>>>>> exercise2/branchB 
18    return phrase 
19 
20print(getPhrase(sys.argv[1]))

Resolution

Correct resolution might be something like this:

Listing 4.4:Exercise 2 - resolution
1#!/usr/bin/python 
2 
3import sys 
4 
5colors = {"black": "black mirror", 
6          "white":  "white noise", 
7          "blue": "blue sky"} 
8 
9def getPhrase(color): 
10    if color.lower() not in colors: 
11        sys.stderr.write("Got no phrase for color %s\n" % color) 
12        sys.exit(1) 
13    phrase = colors[color.lower()] 
14    return phrase 
15 
16print(getPhrase(sys.argv[1]))

If you didn’t use the lower() call on line 10, it’s ok... for the time being. I will explain the rationale to add that call later on. You could have done other things like save color to be lowercase before going into the if on line 10 but we will be touching on the things that we could/should do as we go through other topics. Rest assured, this is just a teaser of a conflict.