In [22]:
seek = input("Number to find?")
seek = int(seek)
count = 0
total = 0
found = False
print ("Before", count)
for item in [9, 72, 5, 43, 100, 23, 7] :
count = count + 1
total = total + item
if int(item) == int(seek) :
found = True
if int(item) != int(seek) :
found = False
print (count, item, found)
print ("After", total)
Before 0 1 9 False 2 72 False 3 5 True 4 43 False 5 100 False 6 23 False 7 7 False After 259 False
In [25]:
smallest = None
print ("Before")
for value in [9, 72, 5, 43, 100, 23, 7] :
if smallest is None :
smallest = value
elif value < smallest :
smallest = value
print (smallest, value)
print ("After", smallest)
Before 9 9 9 72 5 5 5 43 5 100 5 23 5 7 After 5
In [20]:
total = 0
count = 0
while True :
number = input("Enter a number: ")
if number == "done" :
break
try :
number = float(number)
count = count + 1
total = float(total) + float (number)
except :
print("Invalid input")
average = total/count
print(count, round(average,2), round(total,2))
4 40.19 160.77
In [23]:
total = 0
count = 0
while True :
number = input("Enter a number: ")
if number == "done" :
break
try :
number = float(number)
except :
print("Invalid input")
continue
count = count + 1
total = float(total) + float (number)
average = total/count
print('Count:', count, 'Average:', round(average,2), 'Total:', round(total,2))
Invalid input
Count: 3 Average: 5.0 Total: 15.0
In [ ]: