[python作业] [第四周]

7-3 10 的整数倍:让用户输入一个数字,并指出这个数字是否是 10 的整数倍。

代码:

num = input('Please input a number, and I will judge if it is a multiple of 10 q(enter Q to quit):  ')

while num != 'Q' and num != 'q':
    if int(num) % 10 == 0:
        print(num + ' is a multiple of 10')
    else:
        print(num + ' is not a multiple of 10')
    num = input('Please input a number, and I will judge if it is a multiple of 10(enter Q to quit):  ')

测试结果:

Please input a number, and I will judge if it is a multiple of 10 (enter Q to quit):  11
11 is not a multiple of 10
Please input a number, and I will judge if it is a multiple of 10(enter Q to quit):  10
10 is a multiple of 10
Please input a number, and I will judge if it is a multiple of 10(enter Q to quit):  100
100 is a multiple of 10
Please input a number, and I will judge if it is a multiple of 10(enter Q to quit):  Q

7-5 电影票:有家电影院根据观众的年龄收取不同的票价:不到 3 岁的观众免费;
3~12 岁的观众为 10 美元;超过 12 岁的观众为 15 美元。请编写一个循环,在其中询问
用户的年龄,并指出其票价。

代码:

print('This movie theater charges different ticket prices depending on your age.')
age = int(input('Please input your age: '))

if age < 3:
    print('For you, the ticket is free')
elif age < 12:
    print('For you, the ticket is $10')
else:
    print('For you, the ticket is $15')

测试结果:

This movie theater charges different ticket prices depending on your age.
Please input your age: 11
For you, the ticket is $10

7-7 无限循环:编写一个没完没了的循环,并运行它(要结束该循环,可按 Ctrl +C,
也可关闭显示输出的窗口)。
代码:

num = 0
while True:
    print(num)
    num += 1

测试结果:

...
20684
20685
20686
20687
20688
20689
20690
20691
20692
20693
20694
20695
20696
...

7-8 熟食店:创建一个名为 sandwich_orders 的列表,在其中包含各种三明治的名
字;再创建一个名为 finished_sandwiches 的空列表。遍历列表 sandwich_orders,对于
其中的每种三明治,都打印一条消息,如 I made your tuna sandwich,并将其移到列表
finished_sandwiches。所有三明治都制作好后,打印一条消息,将这些三明治列出来。

代码:

sandwitch_orders = ['Egg Mayo', 'Prawn Mayo', 'Tuna Mayo']
finished_sandwiches = []

for each in sandwitch_orders:
    print('I made your ' + each + ' sandwich')
    finished_sandwiches.append(each)

print("\nWe've make these sandwiched:")
for each in finished_sandwiches:
    print(each + ' sandwich')

测试结果:

We've make these sandwiched:
Egg Mayo sandwich, Prawn Mayo sandwich, Tuna Mayo sandwich,
C:\Users\10122\Desktop>python temp.py
I made your Egg Mayo sandwich
I made your Prawn Mayo sandwich
I made your Tuna Mayo sandwich

We've make these sandwiched:
Egg Mayo sandwich
Prawn Mayo sandwich
Tuna Mayo sandwich

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

代码:

def favorite_book(title):
    print('One of my favorite book is ' + title)

favorite_book('A Brief History of Time')

测试结果:

One of my favorite book is A Brief History of Time

8-3 T 恤:编写一个名为 make_shirt()的函数,它接受一个尺码以及要印到 T 恤上
的字样。这个函数应打印一个句子,概要地说明 T 恤的尺码和字样。
使用位置实参调用这个函数来制作一件 T 恤;再使用关键字实参来调用这个函数。

代码:

def make_shirt(size, text):
    print('The size of the shirt is ' + size + ' and the characters on the shirt are ' + text )

make_shirt('XL', 'HelloWorld')
make_shirt(text='Hello', size='L')

测试结果:

The size of the shirt is XL and the characters on the shirt are HelloWorld
The size of the shirt is L and the characters on the shirt are Hello

8-4 大号 T 恤:修改函数 make_shirt(),使其在默认情况下制作一件印有字样“I love
Python”的大号 T 恤。调用这个函数来制作如下 T 恤:一件印有默认字样的大号 T 恤、
一件印有默认字样的中号 T 恤和一件印有其他字样的 T 恤(尺码无关紧要)。

代码:

def make_shirt(size, text='I love Python'):
    print('The size of the shirt is ' + size + ' and the characters on the shirt are ' + text )

make_shirt('L')
make_shirt('M')
make_shirt('XL', 'ipython')

测试结果:

The size of the shirt is L and the characters on the shirt are I love Python
The size of the shirt is M and the characters on the shirt are I love Python
The size of the shirt is XL and the characters on the shirt are ipython

8-6 城市名:编写一个名为 city_country()的函数,它接受城市的名称及其所属的
国家。这个函数应返回一个格式类似于下面这样的字符串:
“Santiago, Chile”

代码:

def city_country(city, country):
    return city + ', ' + country

print(city_country('Guangzhou', 'China'))

测试结果:

Guangzhou, China

猜你喜欢

转载自blog.csdn.net/ill__world/article/details/79732314