嗨,大家好我正在研究我的assigemnt十个绿色瓶子,但每一个罚款,直到最后一节需要有“一个绿色的瓶子”而不是“一个绿色的瓶子”,我也将如何使用.capitalize()
def main():
numbers = [
'no',
'One',
'Two',
'Three',
'Four',
'Five',
'Six',
'Seven',
'Eight',
'Nine',
'Ten', ]
text_one = 'green bottles\nHanging on the wall'
text_two = "\nAnd if one green bottle\nShould accidentally fall\nThere'll be"
text_three =' \n'
# Each iteration of this loop prints one verse of the song
for i in range(10, 0, -1):
file=open('ten.txt', 'a')
file.write(numbers[i]+' ')
file.write(text_one+'\n')
file.write(numbers[i]+' ')
file.write(text_one+' ')
file.write(text_two+' ')
file.write(numbers[i-1]+' ')
file.write(text_one+'\n')
file.write('\n')
file.close()
if __name__ == '__main__':
main()
答案 0 :(得分:0)
这会让你更接近:
numbers = ['Ten', 'Nine', 'Eight', 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'One', 'no']
# variable to add s when needed
plural = "s"
# loop to all but last elements starting at Ten
for i, n in enumerate(numbers[:-1]):
# if n is One we we don't need s
if i > 8:
plural = ""
# get next element for second part of string
nxt = numbers[i+1]
text_one = '{w} green bottle{pl}\nHanging on the wall'.format(w=n, pl=plural)
# if the next element is One we also need don't need s for text_two
if nxt == "One":
plural = ""
text_two = "And if one green bottle\nShould accidentally fall\nThere'll be {nxt} green bottle{pl}"\
.format(nxt=nxt.lower(),pl=plural)
print(text_one+text_two+' hanging on the wall\n')
您需要根据列表中的位置向s
添加bottle
。同样重要的是下一个元素是什么,当n为2时,我们需要bottles
和bottle
。
要写入文件,只需使用write:
替换printnumbers = ['Ten', 'Nine', 'Eight', 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'One', 'no']
plural = "s"
with open("out.txt","w") as f:
for i, n in enumerate(numbers[:-1]):
if i > 8:
plural = ""
nxt = numbers[i+1]
text_one = '{w} green bottle{pl}\nHanging on the wall'.format(w=n, pl=plural)
if nxt == "One":
plural = ""
text_two = "And if one green bottle\nShould accidentally fall\nThere'll be {nxt} green bottle{pl}"\
.format(nxt=nxt.lower(),pl=plural)
f.write(text_one+text_two+' hanging on the wall\n\n')
答案 1 :(得分:0)
这是另一种令人讨厌的不整洁的做法。对现有代码进行最小的更改。
基本上它允许复数s
在字符串中是可选的。
text_one = 'green bottle%s\nHanging on the wall'
%s
充当s
的占位符,必须在以后知道使用
f.write(text1 % 's')
要包含s
或
f.write(text1 % '')
不包含s
。
完整代码:
def main():
numbers = [
'no',
'One',
'Two',
'Three',
'Four',
'Five',
'Six',
'Seven',
'Eight',
'Nine',
'Ten']
text_one = 'green bottle%s\nHanging on the wall'
text_two = "\nAnd if one green bottle\nShould accidentally fall\nThere'll be"
text_three =' \n'
# Each iteration of this loop prints one verse of the song
with open('ten.txt', 'w') as f:
for i in range(10, 0, -1):
f.write(numbers[i]+' ')
if i == 1:
f.write(text_one % '' +'\n')
else:
f.write(text_one % 's'+'\n')
f.write(numbers[i]+' ')
if i == 1:
f.write(text_one % '' +'\n')
else:
f.write(text_one % 's'+'\n')
f.write(text_two+' ')
f.write(numbers[i-1]+' ')
if (i - 1) == 1:
f.write(text_one % '' +'\n')
else:
f.write(text_one % 's'+'\n')
f.write('\n\n')
if __name__ == '__main__':
main()
此代码不是很易维护,但它应该演示该技术。