我正在用Python进行练习,告诉我创建一个名为餐厅的类以及名称和菜肴类型的属性,我成功地完成了练习,但我想知道我能做些什么来制作第一个字母输出中餐厅名称大写 (即麦当劳 -> 麦当劳;河粉 -> 河粉)
我的回答:
class Restaurant:
def __init__ (self, restaurant_name, cuisine_type):
self.name = restaurant_name
self.type = cuisine_type
def describe_restaurant(self):
print(f"This restaurants name is: '{self.name}' and serves {self.type}")
def restaurant_open(self):
print(f"'{self.name}' is currently open")
res1 = Restaurant('pho hung','vietnamese pho')
res1.describe_restaurant()
res2 = Restaurant('macdonalds','american fast')
res2.describe_restaurant()
res2.restaurant_open()
TLDR:如何使类方法打印大写字符串作为输出
这个问题的答案非常简单。
class Restaurant:
def __init__ (self, restaurant_name, cuisine_type):
self.name = restaurant_name
self.type = cuisine_type
def describe_restaurant(self):
print(f"This restaurant's name is: '{self.name.title()}' and serves {self.type}")
def restaurant_open(self):
print(f"'{self.name.title()}' is currently open")
res1 = Restaurant('pho hung', 'vietnamese pho')
res1.describe_restaurant()
res2 = Restaurant('macdonalds', 'american fast')
res2.describe_restaurant()
res2.restaurant_open()
输出将是:
This restaurant's name is: 'Pho Hung' and serves vietnamese pho
This restaurant's name is: 'Macdonalds' and serves american fast
'Macdonalds' is currently open
参考: