In: Computer Science
b) Assume that someone has collected a set of measurements and wants some statistical data about them. Write a program that asks a user for measurements and prints the average, the maximum, and minimum measurement. User should be allowed to enter as many measurements as they want, until entering a negative measurement. The negative measurement should not be processed, but is just used to indicate that the user has finished entering measurements. (Do not use a list to store the measurements) (Working in Python 3.7 (Spyder))
c) For numbers from 2 to 100 print a series of lines indicating which numbers are divisors of other numbers. For each, print out "X divides Y", where X<=Y, and both X and Y are between 2 and 100. First few lines will be 2 divides 2, 3 divides 3...etc (Working in Python 3.7)
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
Note: If second part is not what you are looking for. Just let me know.
#code for first part
#initializing total, count to 0
total=0
count=0
#initializing max and min to None
max=None
min=None
n=0
#looping until n is not negative
while n>=0:
#getting input
n=float(input('Enter a measurement (or a negative value to quit): '))
#checking if n is 0 or positive
if n>=0:
#adding to total, updating count
total+=n
count+=1
#updating max if n is greater than max or max's value is not initialized yet
if max==None or n>max:
max=n
# updating min if n is less than min or min's value is not initialized yet
if min==None or n<min:
min=n
#finding average if count is above 0
avg=0
if count>0:
avg=total/count
#displaying average, max and min
print('Average value: ',avg)
print('Maximum: ',max)
print('Minimum: ',min)
#code for second part
#looping from i=2 to i=100
for i in range(2,100+1):
#looping from j=i to j=100
for j in range(i,100+1):
#checking if i divides j evenly
if j%i==0:
#displaying i and j
print('{} divides {}'.format(i,j))
#output 1
Enter a measurement (or a negative value to quit): 12
Enter a measurement (or a negative value to quit): 13
Enter a measurement (or a negative value to quit): 15
Enter a measurement (or a negative value to quit): 100
Enter a measurement (or a negative value to quit): 4
Enter a measurement (or a negative value to quit): 2.1
Enter a measurement (or a negative value to quit): 889
Enter a measurement (or a negative value to quit): 23
Enter a measurement (or a negative value to quit): 13
Enter a measurement (or a negative value to quit): 456
Enter a measurement (or a negative value to quit): 2
Enter a measurement (or a negative value to quit): 34
Enter a measurement (or a negative value to quit): -34
Average value: 130.25833333333333
Maximum: 889.0
Minimum: 2.0
#output 2 (partial)
2 divides 2
2 divides 4
2 divides 6
2 divides 8
2 divides 10
2 divides 12
2 divides 14
2 divides 16
2 divides 18
…
…
…
89 divides 89
90 divides 90
91 divides 91
92 divides 92
93 divides 93
94 divides 94
95 divides 95
96 divides 96
97 divides 97
98 divides 98
99 divides 99
100 divides 100