- 1 依次输入5个姓名,并且将姓名添加到表,
最后输出The name are .
name_list = []
print"Enter 5 name:(enter a name before Enter)"
for i in range(5):
name=raw_input()
name_list.append(name)
print"The names are",name_list
Enter 5 name:(enter a name after Enter)
Tony
Jenny
Michel
John
Mary
The names are ['Tony', 'Jenny', 'Michel', 'John', 'Mary']```
nameList=[]
print"Enter 5 names (press the Enter key after each name):"
for i in range(5):
name= raw_input()
nameList.append(name)
print '''The names are:''',
print nameList[0],
print nameList[1],
print nameList[2],
print nameList[3],
print nameList[4]
- 2 对上一题的姓名排序
name_List=[]
print"Enter 5 names (press the Enter key after each name):"
for i in range(5):
name= raw_input()
name_List.append(name)
name_List.sort()
print '''The names are:''',
print name_List[0],
print name_List[1],
print name_List[2],
print name_List[3],
print name_List[4]
Enter 5 names (press the Enter key after each name):
Tony
Michel
Jenny
Mary
John
The names are: Jenny John Mary Michel Tony```
学习体会:笨办法,这样可以输出姓名,去掉[]
- 3 对第1题修改,在最后的输出只输出表格里的第3个名字。
name_List=[]
print"Enter 5 names (press the Enter key after each name):"
for i in range(5):
name= raw_input()
name_List.append(name)
print '''The third name you entered is:''',
print name_List[2]
The third name you entered is: Tony```
- 4 修改第1题,让用户可以替换一个新名字进表格里
name_List=[]
print"Enter 5 names (press the Enter key after each name):"
for i in range(5):
name= raw_input()
name_List.append(name)
print '''The names are:''',
print name_List[0],
print name_List[1],
print name_List[2],
print name_List[3],
print name_List[4]
name_index =int(raw_input("Replace one name. Which one? (1-5):"))
new_name=raw_input("New_name:")
name_List.insert(name_index - 1,new_name)
del name_List[name_index]
print '''The names are:''',
print name_List[0],
print name_List[1],
print name_List[2],
print name_List[3],
print name_List[4]
Enter 5 names (press the Enter key after each name):
Tony
Paul
Nick
Michel
Kevin
The names are: Tony Paul Nick Michel Kevin
Replace one name. Which one? (1-5):4
New_name:peter
The names are: Tony Paul Nick peter Kevin
学习体会:这题很烧脑
- 5 编写一个字典程序,让用户可以添加单词和定义,然后可以查找这些单词。确保当要查找的单词不存在时,用户能够知晓。运行的时候,它应该是像这样的:
word_List={}
a = raw_input("Add or look up a word (a/l)?")
if a == "a":
word_type = raw_input("Type the word:")
word_define = raw_input("Type the definition:")
word_List[word_type] = word_define
print "Word added!"
elif a=="l":
word_type = raw_input("Type the word:")
if word_type == word_List:
print "Type the definition:",word_define
else:
print "That word isn't in the dictionary yet."
```
学习体会 :这题也是烧脑的,但是我就思考,照着题目,需要两个功能,一个添加,一个是查询,正好可以用条件判断,并执行添加或是查询的操作。我还希望可以执行完后自动循环,直到如果 输入 l 停止,如何循环我还不知道怎么写。