以下是一个示例代码,用于根据字母值选择选项,并计算平均答案。假设选项为字母A到E,字母值为1到5。
# 定义选项和对应的字母值
options = {
'A': 1,
'B': 2,
'C': 3,
'D': 4,
'E': 5
}
# 获取用户输入的选项
answers = input("请输入选项(用逗号分隔):").split(',')
# 根据字母值选择选项,并计算平均答案
total = 0
count = 0
for answer in answers:
if answer in options:
total += options[answer]
count += 1
if count > 0:
average = total / count
print(f"平均答案为:{average}")
else:
print("无效的选项")
在上面的示例中,我们首先定义了一个字典options
,其中包含选项和对应的字母值。然后,通过input()
函数获取用户输入的选项,使用split()
函数将输入的字符串拆分成一个列表。接下来,使用循环遍历每个用户输入的选项,在字典options
中查找对应的字母值,并累加到total
变量中。同时,使用count
变量记录有效选项的个数。
最后,通过判断count
是否大于0,计算平均答案并打印输出。
请注意,上述示例假设用户输入的选项都是有效的,如果用户输入了无效的选项,则需要根据实际情况进行错误处理。