Python 3.x 找出人们喝咖啡的平均数量——几乎正确(在异常处理方面有一些问题)

Python 3.x 找出人们喝咖啡的平均数量——几乎正确(在异常处理方面有一些问题),python-3.x,Python 3.x,编写一个程序,从标准输入中读取咖啡杯的名称和数量,以查找: 人们喝咖啡的平均数量 喝咖啡少于平均数量的人数 程序应提示用户输入咖啡的名称和数量:。用户可以输入咖啡杯的名称和整数,并用空格分隔。程序将继续要求用户输入,直到用户停止输入 当用户输入stop时,程序应将每个人喝的咖啡的平均数量打印到小数点后2位,以及喝的咖啡少于平均数量的人数。如果在用户输入stop之前没有输入有效的人数,只需打印“没人喝咖啡” 用户输入中需要进行异常处理。用户输入无效输入后,程序应继续提示用户。输入必须仅包含咖啡杯的

编写一个程序,从标准输入中读取咖啡杯的名称和数量,以查找:

人们喝咖啡的平均数量

喝咖啡少于平均数量的人数

程序应提示用户输入咖啡的名称和数量:。用户可以输入咖啡杯的名称和整数,并用空格分隔。程序将继续要求用户输入,直到用户停止输入

当用户输入stop时,程序应将每个人喝的咖啡的平均数量打印到小数点后2位,以及喝的咖啡少于平均数量的人数。如果在用户输入stop之前没有输入有效的人数,只需打印“没人喝咖啡”

用户输入中需要进行异常处理。用户输入无效输入后,程序应继续提示用户。输入必须仅包含咖啡杯的名称和数量,并用空格分隔

成本(输入中的第二个元素)不能为负数

我希望程序在每次输入后打印“无效输入”,而不是在发出“停止”命令后打印。你能帮我修改程序,让它产生预期的结果吗

k=[]
names=[]
nums=[]
while True:
    n=input('Enter name and number of coffees: ')
    if n=='stop':
        break
    if n!='stop':
        k.append(n)
i=0
try:
    i=0
    while i<len(k):
        u=k[i].split()
        names.append(u[0])
        if int(u[1])>0:
            nums.append(int(u[1]))
        else:
            print('Invalid input. {} cannot drink negative cups of coffee.'.format(u[0]))
        i+=1
except ValueError:
    print('Invalid input. Number of coffees must be an integer.')
except IndexError:
    print('Invalid input. Requires name and number.')

try:
    average=sum(nums)/len(nums)
except ZeroDivisionError:
    print('No one drinks coffee.')
    exit()
k=0
count=0
while k<len(nums):
    nu=nums[k]
    if nu<average:
        count+=1
    k+=1
print('The average number of coffees is {:.2f}.'.format(average))
if count==0:
    print('0 people drink less than the average number of coffees.')
if count==1:
    print('{} person drinks less than the average number of coffees.'.format(count))
if count >1:
    print('{} people drink less than the average number of coffees.'.format(count))
实际结果1:

Enter name and number of coffees: Daniel
Invalid input. Requires name and number.
Enter name and number of coffees: stop
No one drinks coffee.
Enter name and number of coffees: Daniel
Enter name and number of coffees: stop
Invalid input. Requires name and number.
No one drinks coffee.
预期结果2:

Enter name and number of coffees: Gillian none
Invalid input. Number of coffees must be an integer.
Enter name and number of coffees: stop
No one drinks coffee.
Enter name and number of coffees: Gillian none
Enter name and number of coffees: stop
Invalid input. Number of coffees must be an integer.
No one drinks coffee.
实际结果2:

Enter name and number of coffees: Gillian none
Invalid input. Number of coffees must be an integer.
Enter name and number of coffees: stop
No one drinks coffee.
Enter name and number of coffees: Gillian none
Enter name and number of coffees: stop
Invalid input. Number of coffees must be an integer.
No one drinks coffee.
预期结果3:

Enter name and number of coffees: Adam -2
Invalid input. Adam cannot drink negative cups of coffee.
Enter name and number of coffees: stop
No one drinks coffee.
Enter name and number of coffees: Adam -2
Enter name and number of coffees: stop
Invalid input. Adam cannot drink negative cups of coffee.
No one drinks coffee.
实际结果3:

Enter name and number of coffees: Adam -2
Invalid input. Adam cannot drink negative cups of coffee.
Enter name and number of coffees: stop
No one drinks coffee.
Enter name and number of coffees: Adam -2
Enter name and number of coffees: stop
Invalid input. Adam cannot drink negative cups of coffee.
No one drinks coffee.
预期结果4:

Enter name and number of coffees: Adam -10
Invalid input. Adam cannot drink negative cups of coffee.
Enter name and number of coffees: Darcy
Invalid input. Requires name and number.
Enter name and number of coffees: Gillian $ten
Invalid input. Number of coffees must be an integer.
Enter name and number of coffees: stop
No one drinks coffee.
Enter name and number of coffees: Adam -10
Enter name and number of coffees: Darcy
Enter name and number of coffees: Gillian $ten
Enter name and number of coffees: stop
Invalid input. Adam cannot drink negative cups of coffee.
Invalid input. Requires name and number.
No one drinks coffee.
实际结果4:

Enter name and number of coffees: Adam -10
Invalid input. Adam cannot drink negative cups of coffee.
Enter name and number of coffees: Darcy
Invalid input. Requires name and number.
Enter name and number of coffees: Gillian $ten
Invalid input. Number of coffees must be an integer.
Enter name and number of coffees: stop
No one drinks coffee.
Enter name and number of coffees: Adam -10
Enter name and number of coffees: Darcy
Enter name and number of coffees: Gillian $ten
Enter name and number of coffees: stop
Invalid input. Adam cannot drink negative cups of coffee.
Invalid input. Requires name and number.
No one drinks coffee.
实际(预期)结果5:

Enter name and number of coffees: Susan 2
Enter name and number of coffees: Peter 3
Enter name and number of coffees: Harold 6
Enter name and number of coffees: stop
The average number of coffees is 3.67.
2 people drink less than the average number of coffees.

基本上,您的方法存在的问题是,它不会在给出输入时处理每个输入。您将所有内容存储在一个数组中,以供以后处理。如果希望得到像
预期输出那样的结果,则每次收到输入时都需要在
while
循环中进行一些处理。我已对您的代码进行了一些修改,请查看:

names=[]
nums=[]
尽管如此:
n=输入('输入咖啡的名称和数量:')
如果n==“停止”:
打破
尝试:
名称,编号=n.split()
除值错误外:
打印('无效输入。需要名称和编号')
其他:
尝试:
咖啡计数=整数(数字)
如果咖啡计数<0:
打印('无效输入。{}不能喝负杯咖啡。'.format(name))
其他:
附加数量(咖啡计数)
name.append(name)
除值错误外:
打印('输入无效。咖啡的数量必须是整数')
尝试:
平均值=总和(nums)/长度(nums)
打印('咖啡的平均数量为{:.2f}.'。格式(平均))
低于平均值=总和([1表示num中的num,如果num<平均值])
打印(“{}人喝的咖啡少于平均咖啡量。”。格式(低于平均值))
除零误差外:
打印(“没有人喝咖啡。”)

提示:您的代码在对任何输入执行任何操作之前都会捕获所有输入。如果您需要在每一行之后给出反馈,则需要在收到每一行时对其进行处理。