|
| 1 | +#Matt Nichols |
| 2 | +#HB2 Lab01 |
| 3 | + |
| 4 | +#Version 1 |
| 5 | + |
| 6 | +#Default list that was given |
| 7 | +nums = [5, 0, 8, 3, 4, 1, 6] |
| 8 | +total = 0 |
| 9 | + |
| 10 | +#For loop for running through the list and getting a 'running sum' |
| 11 | +for num in range(len(nums)): |
| 12 | + running_sum = nums[num] |
| 13 | + total = total + running_sum |
| 14 | + print(total) |
| 15 | + |
| 16 | +#Finding the mean |
| 17 | +mean = total / len(nums) |
| 18 | +print(f'{mean} is the mean') |
| 19 | + |
| 20 | +#Version 2 |
| 21 | + |
| 22 | +#Def function for summing numbers in a list |
| 23 | +def sum(nums): |
| 24 | + total = 0 |
| 25 | + for num in range(len(nums)): |
| 26 | + nums_in_list = nums[num] |
| 27 | + total = total + nums_in_list |
| 28 | + return total |
| 29 | + |
| 30 | +#Def function for user input |
| 31 | +def user_input(): |
| 32 | + user = input("Enter a number (Make sure there are no spaces)\nType 'done' to exit\n") |
| 33 | + return user |
| 34 | + |
| 35 | +#Empty list for sum function |
| 36 | +empty_list = [] |
| 37 | + |
| 38 | +#While loop to take and add the user input to find their running sum |
| 39 | +while True: |
| 40 | + |
| 41 | +#For when the user wants to exit the program |
| 42 | + response = user_input() |
| 43 | + if response == 'done': |
| 44 | + result = "Okay, thank you for your time " |
| 45 | + print(result) |
| 46 | + break |
| 47 | + |
| 48 | +#To add numbers to the empty list and calculate their sum OR to tell them the input was invalid |
| 49 | + try: |
| 50 | + empty_list.append(float(response)) |
| 51 | + added = sum(empty_list) |
| 52 | + print(added) |
| 53 | + except: |
| 54 | + print("Sorry, that wasn't readable. Please make sure you type what is prescribed below. ") |
| 55 | + |
| 56 | +#If statement catching zero input from user, as well as giving them their total/mean if they do give give valuable input |
| 57 | +if len(empty_list) == 0: |
| 58 | + print("Mean = N/A") |
| 59 | +else: |
| 60 | + print(f'Your total is {added}. {added} divided by {len(empty_list)} is equal to {added / len(empty_list)} ') |
0 commit comments