Python Forum
loops in combination with lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
loops in combination with lists
#1
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
Reply
#2
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
Reply
#3
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!
"So, brave knights, if you do doubt your courage or your strength, come no further, for death awaits you all with nasty, big, pointy teeth!" - Tim the Enchanter
Reply
#4
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
Reply
#5
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.
"So, brave knights, if you do doubt your courage or your strength, come no further, for death awaits you all with nasty, big, pointy teeth!" - Tim the Enchanter
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop to find the best combination/score KoinKoin 21 33,223 Jan-05-2023, 10:31 AM
Last Post: KoinKoin
  Algorithm to generate a combination guvinicius2 5 2,724 Aug-15-2020, 10:42 PM
Last Post: deanhystad
  Please Help with lists and loops EthanA 3 2,667 Oct-27-2018, 08:24 AM
Last Post: wavic
  Nested loops, lists and if statements Liquid_Ocelot 10 9,236 Apr-23-2017, 02:02 PM
Last Post: Mekire

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020