python中遇到的问题

img


如何解答,真的不会,有没有可以求解。谢谢,一个编程真的难倒我了,希望有详细解答

max=0
sum=0
count=0
while 1:
    n=input('请输入数值(按回车结束)')
    if n:
        n=float(n)
        sum+=n
        if count==0 or max<n:
            max=n
        count+=1
    else:
        break
print(f'最大值为{max}')
print(f'平均值为{sum/count}')

回答:简单的字符串转换、循环结构与判断,代码如下(这里面只考虑了输入为 int 类型,你可以采用别的类型进行转换)

img

maxValue = -100
aveValue = 0
count = 0
sumValue = 0

while True:
    num = input('请输入数值(直接按回车退出):')
    if num == '':
        break;
    else:
        if int(num) > int(maxValue):
            maxValue = num
        sumValue += int(num)
        count += 1

print('最大值为' + str(maxValue))
print('平均值为' + str(sumValue / count))