城市:编写一个名为describe_city()的函数,它接受一座城市的名字以及该城 市所属的国家。这个函数应打印一个简单的句子,如Reykjavik is in Iceland。给用 于存储国家的形参指定默认值。为三座不同的城市调用这个函数,且其中至少有一座城 市不属于默认国家。
时间: 2024-03-21 20:37:48 浏览: 35
以下是描述城市的函数`describe_city()`的代码:
```python
def describe_city(city, country='中国'):
print(city.title() + "位于" + country + "。")
# 调用函数,指定默认国家
describe_city('北京')
describe_city('上海')
# 调用函数,指定其他国家
describe_city('巴黎', '法国')
```
运行以上代码,输出如下:
```
北京位于中国。
上海位于中国。
巴黎位于法国。
```
可以看到,`describe_city()`函数可以接受一个城市名和一个国家名作为参数,并打印一个简单的句子来描述这个城市所属的国家。在调用函数时,可以指定一个默认国家,如果不指定则默认为“中国”。在本例中,我们首先调用函数分别指定了“北京”和“上海”这两座中国城市,然后再调用函数指定了位于“法国”的城市“巴黎”。
相关问题
创建一个名为 University的类,通过构造函数设置属性university_name 和 university_city。创建一个名为 describe university()的方法,输出大学的姓名和大学所在的城市,创建一个名为 enter _university()的方法,输出一条消息:欢迎来自五湖四海的学子。 根据这个类创建一个名为 university 的实例,分别输出其两个属性,再调用前述两个方法。 作业附件: 共0个
好的,我知道了。以下是实现代码:
```python
class University:
def __init__(self, university_name, university_city):
self.university_name = university_name
self.university_city = university_city
def describe_university(self):
print("大学名称:{},所在城市:{}".format(self.university_name, self.university_city))
def enter_university(self):
print("欢迎来自五湖四海的学子!")
university = University("清华大学", "北京")
print("大学名称:", university.university_name)
print("所在城市:", university.university_city)
university.describe_university()
university.enter_university()
```
输出结果:
```
大学名称: 清华大学
所在城市: 北京
大学名称:清华大学,所在城市:北京
欢迎来自五湖四海的学子!
```
创建一个名为Restaurant的类: 其构造方法_init_()设置两个属性:restaurant_name和cuisine_type。创建一个名为describe_restaurant()的方法,打印前述两项信息 创建一个名为open_restaurant()的方法,打印一条消息,指出餐馆正在营业。
好的,以下是符合您要求的Python代码:
```python
class Restaurant:
def __init__(self, restaurant_name, cuisine_type):
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
print(f"The restaurant's name is {self.restaurant_name}.")
print(f"The cuisine type is {self.cuisine_type}.")
def open_restaurant(self):
print("The restaurant is now open!")
```
您可以通过以下代码来测试上述类:
```python
restaurant = Restaurant("Pizza Hut", "Italian")
restaurant.describe_restaurant()
restaurant.open_restaurant()
```
输出结果为:
```
The restaurant's name is Pizza Hut.
The cuisine type is Italian.
The restaurant is now open!
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)