擅长:python、mysql、java
<p>你可以用字典简化听力时间选项。打开文件的好方法是使用'with'块。在</p>
<pre><code>print("Please, choose from one of the following: ")
listening_times = {1:"One hour a day",
2:"About two hours per day",
3:"Three to four hours per day",
4:"Most of the day"}
for k in listening_times:
print " %d - %s" % (k, listening_times[k])
how_often = int(input("I find myself listening to music..."))
with open("music.txt", 'a') as f:
f.write(listening_times[how_often])
</code></pre>