python - 如何使用另一个函数的结果?
问题描述
我的代码从用户那里获取数字(等级)列表,并根据用户给出的数字找到平均值。找到平均值后,我想将平均值转换为基于字母的成绩。例如,90 的平均值将返回“A”,而 80 将返回“B”。
问题是我不能使用 calculated_average(x)
函数的结果(平均值)并将其用于assign_grade()
.
有小费吗?
#Gets a list of numbers from user
def get_score():
score_list = []
keep_going = 'y'
while keep_going == 'y':
score = float(input('Enter a test score: '))
while score < 0:
print('Positive numbers only')
score = float(input('Enter a test score: '))
score_list.append(score)
keep_going = input("More scores (y/n) ")
return score_list
#Calculates the average
def calculated_average(x):
return sum(x) / len(x)
def assign_grade():
def main():
score = get_score()
print(calculated_average(score))
main()
解决方案
尝试执行以下代码。assign_grade
功能在这里非常基本,但您可以根据需要对其进行编辑:
def get_score():
score_list = []
keep_going = 'y'
while keep_going == 'y':
score = float(input('Enter a test score: '))
while score < 0:
print('Positive numbers only')
score = float(input('Enter a test score: '))
score_list.append(score)
keep_going = input("More scores (y/n) ")
return score_list
#Calculates the average
def calculated_average(x):
return sum(x) / len(x)
def assign_grade(x):
if x>80:
return 'A'
else:
return 'B'
def main():
score = get_score()
avg = calculated_average(score)
letter = assign_grade(avg)
return (letter, avg)
final = main()
print(final)
输出(带输入 85):
print(final)
('A', 85.0)
推荐阅读
- java - BigQuery 写入 API Java - PERMISSION_DENIED:资源上的权限“TABLES_UPDATE_DATA”被拒绝
- snowflake-cloud-data-platform - Snowflake SQL 中的子字符串替换
- anylogic - 独立应用程序无法在 Chrome 版本 92.0.4515.107 中启动
- javascript - HTML Wow.js 不工作(错误的初始化或错误的脚本调用?)
- amazon-web-services - Amazon Athena - current_date - 作为日期格式的列值差异
- ios - 当应用程序处于关闭状态时,不会出现 Sinch VoIP 推送通知
- node.js - 无法部署到heroku
- c# - 使用 JWT 的 Web API 身份用户在两个 API 上共享相同的 Auth 参数
- node.js - 如何通过 TLS 获取 TCP 上的主机名信息
- google-apps-script - GAS - Google Docs API - getActiveDocument().getCursor().getOffset() 在行尾为光标返回意外的“1”