第一篇帖子对我来说很容易。我正努力去当我上课的时候餐馆的名字像标题一样回来。我遇到的问题是乔,当我使用title()时,它以Joe'S的形式返回资本S.当我使用大写()时,乔回来了,但是汉堡王作为汉堡王以小写k回来。我试图找出如何简化这一点,这样我就可以得到每个单词的大写字母,而不是在撇号后大写S.我正在研究的例子来自Python Crash Course第9章。我使用python版本3.xx运行Geany。感谢您的帮助。
class Restaurant():
def __init__(self, restaurant_name, cuisine_type):
"""Initialize name and cuisine type"""
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
print(self.restaurant_name.title() + " serves " + self.cuisine_type)
def open_restaurant(self):
print(self.restaurant_name.capitalize() + " is now open!")
restaurant = Restaurant('joe\'s', 'mexican')
burger_king = Restaurant('burger king', 'burgers')
restaurant.describe_restaurant()
restaurant.open_restaurant()
burger_king.describe_restaurant()
burger_king.open_restaurant()
答案 0 :(得分:0)
只需拆分并加入open_restaurant
即可class Restaurant():
def __init__(self, restaurant_name, cuisine_type):
"""Initialize name and cuisine type"""
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
print(self.restaurant_name.title() + " serves " + self.cuisine_type)
def open_restaurant(self):
Name = self.restaurant_name
print(' '.join([x.capitalize() for x in Name.split(' ')]) + " is now open!")
restaurant = Restaurant('joe\'s', 'mexican')
burger_king = Restaurant('burger king', 'burgers')
restaurant.describe_restaurant()
restaurant.open_restaurant()
burger_king.describe_restaurant()
burger_king.open_restaurant()