Lists – WSQ10

Lists – WSQ10

The program must ask the user for ten numbers, tell the user they cannot enter any more values, then print the total sum, average and standard deviation of these. So, first, to make sure the user is only able to enter 10 values, you have to set up a loop, in this case I used the while loop, in which, as long as the counter x is lower or equal to 10 (except it respected the 10 values permitted only with < rather than <= or changing it to 11, so 10 it is), it will continue to receive inputs.

Onto the list, I opted to define the list outside the loop as an empty list, that way the variable that contains the numbers given by the user will be appended to l (the list), it allows me to save myself from creating a long chunk of code with lists that would contain each input, also it’s cleaner and more organized this way for me.

After that, since we want to do this using functions because functions are fun, we’re defining the first one (this was explained in the previous entry pal), I’m using another counter here for the total sum, so I can add to it the value of m (which is the equivalent for l, below) for as long as the loop goes on, to make sure the function is gonna do its job no matter how many numbers the user is permitted to enter, we indicate a range, which will depend on the length of m. When you’re thorough with that, make sure to add a variable that contains that function, below the loop. All that business was only for the total sum of the numbers.

For the average of these, we create another function, since we did most of the work in the first function, all we need to do now is use the variable that contains the total sum function and divide it by 10.0 (since it’s a float value), to obtain its average. We do the same with this function and save it in another variable below the total sum.

Finally, there is actually a specific preset function for standard deviation, but before you use it you must import it at the very beginning of your code, this way you can now use it in the list, I’m not entirely sure why I couldn’t use the variable for the average function that I saved below in this one though, it only worked for me directly using l (for list), also when it comes to indexation, I had to take this one out of the loop otherwise it wouldn’t work.

And that’s basically it, the last part is just printing the results on screen, always remembering to use the names of the variables that contain their respective functions rather than said functions or variables inside these.

Github code link.

Lists – WSQ10

CC BY 4.0 Lists – WSQ10 by tywins is licensed under a Creative Commons Attribution 4.0 International License.

Comments are closed.