python作业04-输入&函数

Description (input)

Select from exercise 7-1 to 7-10

Code (input)

7-3

1
2
3
4
5
6
7
# 7-3 10的整数倍 :让用户输入一个数字,并指出这个数字是否是10的整数倍。
for i in range(1, 4):
num = int(input("Enter a number: "))
if (num % 10 == 0):
print("Yes")
else:
print("No")

result:

1
2
3
4
5
6
7
8
9
Enter a number: 120
Yes
Enter a number: 3
No
Enter a number: f
Traceback (most recent call last):
File "D:/pyproject/homework1/7.py", line 3, in <module>
num = int(input("Enter a number: "))
ValueError: invalid literal for int() with base 10: 'f'

7-7

1
2
3
4
5
# 7-7 无限循环 :编写一个没完没了的循环,并运行它(要结束该循环,可按Ctrl +C,也可关闭显示输出的窗口)。
i = 0
while(1):
print(i)
i = i+1;

result:

1
2
3
4
1
2
3
...

7-10

1
2
3
4
5
6
7
8
9
10
# 7-10 梦想的度假胜地 :编写一个程序,调查用户梦想的度假胜地。使用类似于“If you could visit one place in the world,
# where would you go?”的提示,并编写一个打印调查结果的代码块。
cities = []
for i in range(0, 10):
where = input("If you could visit one place in the world, where would you go?")
cities.append(where)

for city in set(cities):
print(city+": ")
print(cities.count(city))

result:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
If you could visit one place in the world, where would you go?HongKong
If you could visit one place in the world, where would you go?America
If you could visit one place in the world, where would you go?HongKong
If you could visit one place in the world, where would you go?Tokyo
If you could visit one place in the world, where would you go?Kyoto
If you could visit one place in the world, where would you go?Tokyo
If you could visit one place in the world, where would you go?Shanghai
If you could visit one place in the world, where would you go?HongKong
If you could visit one place in the world, where would you go?Beside you
If you could visit one place in the world, where would you go?Kyoto
Beside you:
1
Shanghai:
1
Tokyo:
2
Kyoto:
2
HongKong:
3
America:
1

Description (function)

Select from exercise 8-1 to 8-16

Code (function)

8-2

1
2
3
4
5
6
# 8-2 喜欢的图书 :编写一个名为favorite_book() 的函数,其中包含一个名为title 的形参。这个函数打印一条消息,
# 如One of my favorite books is Alice in Wonderland 。调用这个函数,并将一本图书的名称作为实参传递给它。
def favorite_book( title):
print("One of my favorite books is "+title.title())

favorite_book(input("Which book do you like best?"))

result:

1
2
Which book do you like best?harry potter
One of my favorite books is Harry Potter

8-4

1
2
3
4
5
6
7
8
9
10
11
# 8-3 T恤 :编写一个名为make_shirt() 的函数,它接受一个尺码以及要印到T恤上的字样。这个函数应打印一个句子,
# 概要地说明T恤的尺码和字样。使用位置实参调用这个函数来制作一件T恤;再使用关键字实参来调用这个函数。
# 8-4 大号T恤 :修改函数make_shirt() ,使其在默认情况下制作一件印有字样“I love Python”的大号T恤。
# 调用这个函数来制作如下T恤:一件印有默认字样的大号T恤一件印有默认字样的中号T恤和一件印有其他字样的T恤(尺码无关紧要)。

def make_shirt( size, slogen = "I love Python!"):
print("Size: " + size)
print("Print with: "+slogen)

make_shirt("XXL")
make_shirt("L", "Hello world")

result:

1
2
3
4
Size: XXL
Print with: I love Python!
Size: L
Print with: Hello world

8-6

1
2
3
4
5
6
7
8
9
# 8-6 城市名 :编写一个名为city_country() 的函数,它接受城市的名称及其所属的国家。这个函数应返回一个格式类似于下面这样的字符串:
# "Santiago, Chile"
# 至少使用三个城市-国家对调用这个函数,并打印它返回的值。
def city_country(country, city):
return city+", "+country

print(city_country("China", "Guangzhou"))
print(city_country("Japan", "Tokyo"))
print(city_country("America", "Los Angeles"))

result:

1
2
3
Guangzhou, China
Tokyo, Japan
Los Angeles, America

8-14

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 8-14 汽车 :编写一个函数,将一辆汽车的信息存储在一个字典中。这个函数总是接受制造商和型号,还接受任意数量的关键字实参。这样调用这个函数:提供必不可
# 少的信息,以及两个名称—值对,如颜色和选装配件。这个函数必须能够像下面这样进行调用:
# car = make_car('subaru', 'outback', color='blue', tow_package=True)
def make_car( brand, type, **car_info):
car = {}
car["brand"] = brand
car["type"] = type
for key, value in car_info.items():
car[key] = value
return car

print(make_car("BMW", "gas", color="black", price="100 milions"))
print(make_car("Benz", "gas",))
print(make_car("Tesla", "eletric", appearance="cool"))

result:

1
2
3
{'brand': 'BMW', 'type': 'gas', 'color': 'black', 'price': '100 milions'}
{'brand': 'Benz', 'type': 'gas'}
{'brand': 'Tesla', 'type': 'eletric', 'appearance': 'cool'}