怎么输出s1.count=1 count=1 Student.count=hello s2.name=Tom,s2.age=18 name=Tom age=18 __fee=8000 count=3 __fee=8000 count=3 totalfee=24000 totalfee=24000
时间: 2023-07-29 11:06:23 浏览: 95
你可以将代码稍加修改,如下所示:
```python
def print_counts():
class Student:
count = "hello"
s1 = Student()
print(f"s1.count={s1.count}")
print(f"count={Student.count}")
print(f"Student.count={Student.count}")
def print_info(name, age):
print("s2.name={0}, s2.age={1}".format(name, age))
print("name={0} age={1}".format(name, age))
def print_fee(fee, count):
print("__fee={0} count={1} totalfee={2}".format(fee, count, fee * count))
return fee * count
print_counts()
s2 = {'name': 'Tom', 'age': 18}
print_info(s2['name'], s2['age'])
fee = 8000
count = 3
totalfee1 = print_fee(fee, count)
totalfee2 = print_fee(fee, count)
print("totalfee={}".format(fee*count))
print("totalfee={}".format(fee*count))
```
运行以上代码,输出结果如下:
```
s1.count=hello
count=hello
Student.count=hello
s2.name=Tom, s2.age=18
name=Tom age=18
__fee=8000 count=3 totalfee=24000
__fee=8000 count=3 totalfee=24000
totalfee=24000
totalfee=24000
```
其中,`print_counts()`函数用于输出`Student`类中的`count`属性,`print_info()`函数用于输出`s2`字典中的`name`和`age`键对应的值,`print_fee()`函数用于输出`fee`和`count`的乘积以及计算并返回总费用`fee*count`,最后两行代码用于输出总费用。
阅读全文