英文:
How to improve this program's readability?
问题
我的代码:
friend_1 = {
"first_name": "Tu",
"last_name": "Le",
"age": 18,
"city": "Hanoi"
}
friend_2 = {
"first_name": "Hao",
"last_name": "Do",
"age": 18,
"city": "Hanoi"
}
friend_3 = {
"first_name": "Hieu",
"last_name": "Tran",
"age": 18,
"city": "HCMC"
}
friends = [friend_1, friend_2, friend_3]
for friend in friends:
print(f"\n姓名:{friend['first_name']} {friend['last_name']}\n年龄:{friend['age']}\n城市:{friend['city']}")
输出看起来类似于:
姓名:Tu Le
年龄:18
城市:Hanoi
姓名:Hao Do
年龄:18
城市:Hanoi
姓名:Hieu Tran
年龄:18
城市:HCMC
英文:
I'm learning the basic of Python dictionaries and am having an exercise on nesting dictionaries in a list. It basically told me to make 3 dictionaries about 3 people, nest them inside a list, loop through the list, and print out the info of each person.
I have already had the output I wanted, but my print statement was hard to read with a lot of \n. Can you suggest other ways to improve readability?
My code:
friend_1 = {
"first_name": "Tu",
"last_name": "Le",
"age": 18,
"city": "Hanoi"
}
friend_2 = {
"first_name": "Hao",
"last_name": "Do",
"age": 18,
"city": "Hanoi"
}
friend_3 = {
"first_name": "Hieu",
"last_name": "Tran",
"age": 18,
"city": "HCMC"
}
friends = [friend_1, friend_2, friend_3]
for friend in friends:
print(f"\nFirst name: {friend['first_name'].title()}\nLast name: {friend['last_name'].title()}\nAge: {friend['age']}\nCity: {friend['city'].title()}")
The output looks somewhat like this:
First name: Tu
Last name: Le
Age: 18
City: Hanoi
答案1
得分: 1
假设问题出现在打印语句中,您可以利用打印函数可以接受多个参数的特性,然后指定一个分隔符 sep
,用于定义如何分隔打印语句中包含的所有个体元素
print(
f"First name: {friend['first_name'].title()}",
f"Last name: {friend['last_name'].title()}",
f"Age: {friend['age']}",
f"City: {friend['city'].title()}",
sep="\n"
)
英文:
Assuming the problem is the way it appears in the print statement, you can make use of the fact print can take in lots of arguments to print and then specify a sep
arator that can be used to define how to separate all the individual elements you've included in the print statement
print(
f"First name: {friend['first_name'].title()}",
f"Last name: {friend['last_name'].title()}",
f"Age: {friend['age']}",
f"City: {friend['city'].title()}",
sep="\n"
)
答案2
得分: 1
我知道你已经得到了预期的答案,但我仍然想给你一个小例子,展示如何控制打印数据结构的方式,以及如何避免重复使用某个字符。
```python
class Person:
def __init__(self, first_name: str, last_name: str, age: int, city: str):
self.dict = {"first_name": first_name,
"last_name": last_name,
"age": age,
"city": city}
def __str__(self):
s = "\n".join([f'名字:{self.dict["first_name"].title()}',
f'姓氏:{self.dict["last_name"].title()}',
f'年龄:{self.dict["age"]}',
f'城市:{self.dict["city"]}'])
return s
friend_1 = Person("Hieu", "Tran", 18, "HCMC")
print(friend_1)
英文:
I know you got the expected answer, but I still want to give you a small example of how you can control the way to print your data structure, and how to avoid using a certain character repeatly.
class Person:
def __init__(self, first_name: str, last_name: str, age: int, city: str):
self.dict = {"first_name": first_name,
"last_name": last_name,
"age": age,
"city": city}
def __str__(self):
s = "\n".join([f'Fist name: {self.dict["first_name"].title()}',
f'Last name: {self.dict["last_name"].title()}',
f'Age: {self.dict["age"]}',
f'City: {self.dict["city"]}'])
return s
friend_1 = Person("Hieu", "Tran", 18, "HCMC")
print(friend_1)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论