示例文件和预期结果:
> file_1.txt
A. Aardvark d. C. Capybara
C. Capybara d. B. Badger
C. Capybara d. F. Flamingo
A. Aardvark d. G. Goat
F. Flamingo d. Bye
E. Echidna d. D. Dolphin
A. Aardvark d. E. Echidna
> python spoon.py file_1.txt
No one wins the wooden spoon.
示例文件和预期结果:
> file_2.txt
C. Capybara d. A. Aardvark
C. Capybara d. B. Badger
C. Capybara d. F. Flamingo
A. Aardvark d. G. Goat
F. Flamingo d. Bye
E. Echidna d. D. Dolphin
A. Aardvark d. E. Echidna
> python spoon.py file_2.txt
D. Dolphin wins the wooden spoon!
QF: E. Echidna d. D. Dolphin
SF: A. Aardvark d. E. Echidna
F: C. Capybara d. A. Aardvark
信息:
我做了什么:
的问题:
示例问题情景和预期结果:
> file_3.txt
A. Aardvark d. C. Capybara
A. Aardvark d. E. Echidna
C. Capybara d. F. Flamingo
> python spoon.py file_2.txt
F. Flamingo wins the wooden spoon!
SF: C. Capybara d. F. Flamingo
F: A. Aardvark d. C. Capybara
当前结果:
- Traceback (most recent call last):
- File "woodenspoon.py", line 56, in <module>
- winner, loser = final.split(' d. ')
- NameError: name 'final' is not defined
我目前的守则/进展:
import sys
if len(sys.argv) < 2:
sys.exit("No file specified")
try:
filename = sys.argv[1]
file = open(filename, 'r')
except:
sys.exit("Could not read file")
outcome = []
for line in file:
outcome.append(line.strip())
element_index = len(outcome)-1
round_1 = []
for i in range(0, element_index):
isSame = False
for d in range(i+1, element_index):
org_player_1, org_player_2 = outcome[i].split(' d. ')
comp_player_1, comp_player_2 = outcome[d].split(' d. ')
if org_player_2 == comp_player_1:
isSame = True
if isSame == False:
round_1.append(outcome[i])
for i in range(len(round_1)):
try:
outcome.remove(round_1[i])
except:
pass
element_index = len(outcome)
round_2 = []
for i in range(0, element_index):
isSame = False
for d in range(i+1, element_index):
org_player_1, org_player_2 = outcome[i].split(' d. ')
comp_player_1, comp_player_2 = outcome[d].split(' d. ')
if org_player_2 == comp_player_1:
isSame = True
if isSame == False:
round_2.append(outcome[i])
for i in range(len(round_2)):
try:
outcome.remove(round_2[i])
except:
pass
for e in outcome:
final = e
winner, loser = final.split(' d. ')
for e in round_2:
if loser in e:
semifinal = e
winner, loser = semifinal.split(' d. ')
for e in round_1:
if loser in e:
quaterfinal = e
winner, loser = quaterfinal.split(' d. ')
if "Bye" in loser:
print("No one wins the wooden spoon.")
else:
print("{} wins the wooden spoon!\n".format(loser))
print("QF: {}".format(quaterfinal))
print("SF: {}".format(semifinal))
print("F: {}".format(final))
注意: