RESOURCES:
Class slides: https://docs.google.com/presentation/d/1OzGQZc_DA0LFSBX92oApZWvSX5bW_tTdOiP6mNV2FvM/edit?usp=sharing
Example code: https://replit.com/@ShravyaS/IntroToPython-7
HOMEWORK:
Write a program that repeatedly takes in input as a number until "STOP" is inputted. Find the average of the numbers.
Hint: Have an input inside of the while loop! (Look at the example code for how to do this.)
Write your answer as a comment, and we’ll post the solution by next class. See you then!
Note: VIP students, please contact us (Eric and me) about office hours. And of course, feel free to ask for help!
print('Please input your numbers and then type STOP when you are done'); user_input = input(); num_input =[]; num_average = 0; while (user_input != "STOP"): num_input.append(float(user_input)); user_input = input(); num_average = sum(num_input) / len(num_input); print("User input is done"); print(f"Total number of inputs is {len(num_input)}"); print(f"The sum of all the inputs is {sum(num_input)}"); print(f"The average of the inputs is {num_average}");
print('Please input your numbers and then type STOP when you are done'); num_sum = 0; user_input = input(); num_input = 0; num_average = 0; while (user_input != "STOP"): num_sum = num_sum + float(user_input); user_input = input(); num_input = num_input + 1; num_average = num_sum / num_input; print("User input is done"); print(f"Total number of inputs is {num_input}"); print(f"The sum of all the inputs is {num_sum}"); print(f"The average of the inputs is {num_average}");
Solution:
num=0
len=0
while x!="STOP":
x=int(input())
if x != "STOP":
num = num+x
len+=1
print(sum/len)