-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadieu.py
More file actions
53 lines (41 loc) · 1.81 KB
/
adieu.py
File metadata and controls
53 lines (41 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""
In The Sound of Music, there’s a song sung largely in English, So Long, Farewell, with these lyrics, wherein “adieu” means “goodbye” in French:
Adieu, adieu, to yieu and yieu and yieu
Of course, the line isn’t grammatically correct, since it would typically be written (with an Oxford comma) as:
Adieu, adieu, to yieu, yieu, and yieu
To be fair, “yieu” isn’t even a word; it just rhymes with “you”!
In a file called adieu.py, implement a program that prompts the user for names, one per line, until the user inputs control-d. Assume that the user will input at least one name. Then bid adieu to those names, separating two names with one and, three names with two commas and one and, and
names with
commas and one and, as in the below:
Adieu, adieu, to Liesl
Adieu, adieu, to Liesl and Friedrich
Adieu, adieu, to Liesl, Friedrich, and Louisa
Adieu, adieu, to Liesl, Friedrich, Louisa, and Kurt
Adieu, adieu, to Liesl, Friedrich, Louisa, Kurt, and Brigitta
Adieu, adieu, to Liesl, Friedrich, Louisa, Kurt, Brigitta, and Marta
Adieu, adieu, to Liesl, Friedrich, Louisa, Kurt, Brigitta, Marta, and Gretl
"""
def merge_names(names):
names = ", ".join(names).strip()
names_with_comma = names.rsplit(",", 1)
names = ", and".join(names_with_comma).strip()
return names
def main():
names = []
adieu = "Adieu, adieu, to"
while True:
try:
name = str(input("Name: "))
if name not in names:
names.append(name)
except EOFError:
print()
if len(names) == 1:
print(adieu, names[0])
elif len(names) == 2:
print(adieu, " and ".join(names))
elif len(names) > 2:
print(adieu, merge_names(names))
break
if __name__ == "__main__":
main()