# Second Exercises
# Find a sum until enter 0
OUTPUT
Enter number:
Enter number:
Enter number:
Enter number:
875.0
Solutions
sum=0
while(True):
incoming=input("Enter number: ")
if(incoming == "0"):
break
else:
sum+=float(incoming)
print(sum)
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
or
data=[]
while(True):
incoming=input("Enter number: ")
if(incoming== "0" ):
sum=0
for number in data:
sum+=number
print(sum)
break
else:
data.append(float(incoming))
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# Find a sum average until enter "done"
OUTPUT
Enter a command:
Enter a command:
Enter a command:
avg: 21.5
OUTPUT
Enter a command:
Enter a command:
Enter a command:
Enter a command:
Enter a command:
avg: 169.25
Solution
sum=0
n=0
while(True):
data=input("Enter Number: ")
if(data!="done"):
sum+=float(data)
n+=1
else:
break
print(f"avg : {sum/n}")
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# Find max value
OUTPUT
Enter a command:
Enter a command:
Enter a command:
max: 31.0
OUTPUT
Enter a command:
Enter a command:
Enter a command:
Enter a command:
Enter a command:
max: 60.0
OUTPUT
Enter a command:
Enter a command:
Enter a command:
Enter a command:
Enter a command:
max: -4.0
Solution
current_max = None
while True:
data = input("Enter a command: ")
if(data == "done"):
break
data = float(data)
if(current_max is None):
current_max = data
if(data > current_max):
current_max = data
print(f"max: {current_max}")
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11