《python从入门到实践》部分答案

学到游戏部分,突然发现有些知识点不是很熟悉,出现了对照着书才能把代码写好的状况,所以决定吧前面的习题重新做一遍,算是对知识的复习。
2-1

a = 1
print(a)

3-1

names = ['linger','huangdui','junainai']
for name in names:
    print(name)

3-2

names = ['linger','huangdui','junainai']
for name in names:
    print('welcome,'+name)

3-8

places = ['taiwan','hanguo','meiguo','beijin']
places.sort()# 按字母顺序排列
for place in places:
    print(place)
places.reverse()# 按照原有相反的顺序排列
for place in places:
    print(place)

3-9

places = ['taiwan','hanguo','meiguo','beijin']
print(len(places))

4-3

for value in range(1,21):
    print(value)

4-4

list = []     #为了可以使用sum等的函数,要将数字输入进去
for value in range(1,1000001):
    list.append(value)
sum(list)
print(sum(list))      #只能打印字符

4-6

for value in range(1,21,2): #最后一个2表示步长
    print(value)

4-7

for value in range(3,31):
    if value%3 == 0:   #其中的%是取余函数
        print(value)

4-8

for value in range(3,31):
    print(str(value**3))#    print只能打印字符串,所以要强制把结果变为字符

4-9

#列表解析就是要用好* for * in *
list = [value**2 for value in range(1,11)]
print(list)

4-10

list = [value**2 for value in range(1,11)]
print(list)
print('The first three items in the list are:')
print(list[0:3])
print('Three items from the middle of the list are:')
print(list[3:6])
print('The last items of the list are:')
print(list[6:])

元组中要注意的地方是,元组中的元素不能随便修改,比如

yuanzu = (200,50)
yuanzu[0] = 250
#这一步是在尝试修改元组中的第一个元素
这一步执行的结果就是会报错,要修改只能成对修改

4-13

wjwd = ('mnzwr','xhnrg','ggbc','ymdx','ky')
for cai in wjwd:
    print(cai)
wjwd[1]= 'hahayu'
print(wjwd)

5-6

age = int(input('Please input your age:'))
if age < 2:
    print('You are a baby!')
elif 2 <= age < 4:
    print('You are learning to walk!')
elif 4 <= age < 13:
    print('You are a child!')
elif age >= 13:
    print('You are a young man!')

5-8

current_users = ['admin','lizixuan','jiangweijie','lichao','juran']
for current_user in current_users:
    if current_user == 'admin':
        print('Hello admin,would you like to see a status report?')
    else:
        print('Hello '+current_user+',thank you for logging in again.')

5-9

current_users = ['admin','lizixuan','jiangweijie','lichao','juran']
for current_user in current_users:
    if current_user == 'admin':
        print('Hello admin,would you like to see a status report?')
    else:
        print('Hello '+current_user+',thank you for logging in again.')
current_users = []
if current_user:
    print('we need to find some users!')

6-1

msw ={"first_name":"ma","last_name":"suowen","age":"24","city":"yangzhou"}
for key,value in msw.items():
	print("my friend's "+key+" is "+value)

运行结果:

my friend's first_name is ma
my friend's last_name is suowen
my friend's age is 24
my friend's city is yangzhou
[Finished in 0.1s]

6-7

嵌套()这一题是列表里有字典

peoples = [{'msw':{"first_name":"ma","last_name":"suowen","age":"24","city":"yangzhou"}},{'wp':{"first_name":"wang","last_name":"pei","age":"24","city":"danyang"}}]
for people in peoples:
	for key,value in people.items():
		for key1,valu in value.items():
			print("my friend "+key+"'s"+key1+'is'+valu)

运行结果
my friend msw’sfirst_nameisma
my friend msw’slast_nameissuowen
my friend msw’sageis24
my friend msw’scityisyangzhou
my friend wp’sfirst_nameiswang
my friend wp’slast_nameispei
my friend wp’sageis24
my friend wp’scityisdanyang
[Finished in 0.2s]
7-4

ingredients = []
mark = True
while mark:
	ingredient = input("please input your ingredients:")
	if ingredient != 'quit':
		ingredients.append(ingredient)
	else:
		break
print(ingredients)

运行结果:

jurandeMacBook-Pro:~ juran$ cd python
jurandeMacBook-Pro:python juran$ python3 7-4.py
please input your ingredients:happy
please input your ingredients:hahah
please input your ingredients:nidene
please input your ingredients:cheer up
please input your ingredients:quit
['happy', 'hahah', 'nidene', 'cheer up']
jurandeMacBook-Pro:python juran$ 

猜你喜欢

转载自blog.csdn.net/weixin_43580807/article/details/88133191
今日推荐