Python Forum

Full Version: loops in combination with lists
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I need help. For my homework I need to create a program in which people can insert numbers and my program will first list all the chosen numbers. Then it is supposed to calculate the sum, average and how many times the number five was chosen. I have been able to do this, as shown with the following coding.

#Here the input is organised
number1 = input('Please note your 1st number: ')
number2 = input('Please note your 2nd number: ')
number3 = input('Please note your 3rd number: ')
number4 = input('Please note your 4th number: ')
number5 = input('Please note your 5th number: ')
number6 = input('Please note your 6th number: ')
number7 = input('Please note your 7th number: ')
number8 = input('Please note your 8th number: ')

#Here the list with all the inputs is formed
numberlst = [int(number1), int(number2) ,int(number3), int(number4), int(number5), int(number6), int(number7), int(number8)]
print('This is a list of the chosen numbers ' + str(numberlst))

#here the sum is being calculated
sum_input = sum(numberlst)
print('The sum of the chosen numbers is equal to ' + str(sum_input))

#Then the average is calculated and rounded of 
average = round(float(sum_input) / 8, 1)
print('The average of the chosen numbers is equal to ' + str(average))

#lastly, the amount of fives is calculated
amount_5s = numberlst.count(5)
print('You have chosen a total of {} fives'.format(amount_5s))
But the second assignment was to change this program, so that the number of inputs can be chosen in the beginning. So the user can chose if they want to input 4 or 6 or whatever amount of numbers they want. And that is where I got stuck, my teacher told me to use loop. Yet I wouldn't know how to. If you now how, will you please help me.

regards, Juru
for your loop:
  • create empty list numbers = []
  • use while loop: while True:
  • input statement with text, asking for a digit or Q
  • Check if Q and break if so
  • check if isdigit
  • if so, convert to decimal and append to numbers
  • if not, print warning to use only digits or Q
  • once you exit, do summations, etc on numbers
I'm assuming you need to keep this simple and not use functions. Here is some simple pseudo-code for using a while loop:

Ask user for "How many numbers to process?" and store in max_nums variable.

Initialize an empty list called num_list to hold the numbers. (hint - an empty list = [])

Start an infinite while loop (hint - while True:)
__Check if size of num_list >= max_nums. (hint - use length function - len(num_list)
____if it is,
______break
____if not,
______Ask the user for a new number and append it to your num_list. (hint - Use num_list.append())

Once you fill the list with the required numbers and break out of the loop just use your previous code as before.

You can also do this a bit cleaner with a for loop. Look up the range() function...

Cry Man, it takes 10 times as long to write python pseudo-code as it does to just write the python code. I didn't want to give everything away but if you're still confused post back and I'm sure we can help...


@Larz60+: Great minds think alike but I guess mine thinks a little slower!
Being a stickler for the specification in the assignment, you are supposed to find out at the beginning how many numbers there will be. The "Q" method is even better, more flexible, but is not exactly what was stipulated. Instead you want to input number_of_numbers, then use a for loop (instead of while) to get those numbers, then do your calculations
I would have used a sentinel value like "Q" or "quit" as well but I noticed the teachers request. I'll bet the plan is to do the same thing with a for loop next so it makes good sense to do it this way.