- ‘’‘练习7-4:比萨配料 编写一个循环,提示用户输入一系列比萨配料,并在用户输入’quit’时结束循环。
每当用户输入一种配料后,都打印一条消息,指出我们会在比萨中添加这种配料。’‘’
a=input('请输入配料:')
while a!='quit':
print('我们会在比萨中添加' + a)
a = input('请输入配料:')
'''
定义函数:def function(参数):
代码部分
'''
- ‘’‘练习8-2:喜欢的图书
编写一个名为favorite_book()的函数,其中包含一个名为title的形参。这个函数打印一条消息,下面是一个例子。 One of
my favorite books is Alice in Wonderland.调用这个函数,并将一本图书的名称作为实参传递给它。’
‘’
def favorite_book(title):
a=3
# print('One of my favorite books is '+title)
# print(f'One of my favorite books is {title}')
content="""One of my favorite books is {title1}""".format(title1=title)
print(content)
return a
m=favorite_book('Alice in Wonderland')
print(m)
- ‘’‘""练习8-6:城市名
编写一个名为city_country()的函数,它接受城市的名称及其所属的国家。这个函数应返回一个格式类似于下面的字符串:
“Santiago,Chile” 至少使用三个城市国家对来调用这个函数,并打印它返回的值。 ‘’’
def city_country(city,country):
return f"{city},{country}"#Santiago,Chile
# return city,country
print(city_country('Santiago','Chile'))#('Santiago', 'Chile')