6、函数返回值 P93
def func(arg):
# ....
return 9 # 返回值为9 默认:return None
val = func('adsfadsf')
# 1. 让用户输入一段字符串,计算字符串中有多少A字符的个数。有多少个就在文件a.txt中写多少个“李邵奇”。
def get_char_count(data):
sum_counter = 0
for i in data:
if i == 'A':
sum_counter += 1
return sum_counter
def write_file(line):
if len(line) == 0:
return False # 函数执行过程中,一旦遇到return,则停止函数的执行。
with open('a.txt',mode='w',encoding='utf-8') as f:
f.write(line)
return True
content = input('请输入:')
counter = get_char_count(content)
write_data = "李邵奇" * counter
status = write_file(write_data)
if status:
print('写入成功')
else:
print('写入失败')