You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

创建整数筛选循环遇问题:Python程序报‘Type对象不可迭代’错求助

Fixing Your Number Input & Statistics Program

Hey there! Let's break down what's going wrong with your code and fix it step by step—since you're only three weeks in, this is totally normal to stumble on these logic kinks!

Key Issues in Your Original Code

  • List gets reset every loop: You created our_list = [] inside the while loop, so every time you enter a number, it wipes the previous entries.
  • Early integer conversion: You tried converting the input to int right away, which means entering "done" or a non-number will crash before you can check for it.
  • Misplaced try/except block: Your error handling was outside the loop and targeting the wrong code, plus prompt = type(int) is not how you check input types (that's why you got the "Type object is not iterable" error—you tried looping over a type class!).
  • Incorrect statistics calculation: You were calculating count/total/average inside the loop (which only ever uses one entry) and tried calling total() like a function, which it's not.

Corrected Code

# Initialize empty list OUTSIDE the loop so it doesn't reset
our_list = []
while True:
    user_input = input('Enter a number, or type "done" to finish: ')
    
    # Check if user wants to exit first
    if user_input.lower() == 'done':
        break
    
    # Try converting input to integer, handle errors
    try:
        number = int(user_input)
        our_list.append(number)
    except ValueError:
        print('Please enter a whole number')

# Calculate statistics AFTER collecting all inputs
if our_list:  # Make sure list isn't empty before calculating
    count = len(our_list)
    total = sum(our_list)
    average = total / count
    
    print('\nDone!')
    print(f'Total numbers entered: {count}')
    print(f'Sum of numbers: {total}')
    print(f'Average of numbers: {average:.2f}')  # .2f rounds to 2 decimal places
else:
    print('\nDone! No numbers were entered.')

What Changed & Why

  • Moved list initialization: our_list = [] is now outside the loop, so it keeps all entries as you add them.
  • Checked for "done" first: We get the input as a string first, so we can check if the user wants to stop before trying to convert to a number.
  • Proper try/except placement: The conversion to int is inside the try block, so if it fails (like entering a string or decimal), we catch the ValueError and show the correct message.
  • Calculated stats after loop: We only compute count, total, and average once the user is done entering numbers, so we're using the full list of entries.
  • Added empty list check: If the user types "done" without entering any numbers, we avoid a division by zero error.

That should work exactly how you want it to! Let me know if you have any questions about any part of this.

内容的提问来源于stack exchange,提问作者LetsGetTr0pical

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 11:38:13