Doubt in small python code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Doubt in small python code

Write a program that repeatedly prompts a user for integer numbers until the user enters 'done'. Once 'done' is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number. Enter 7, 2, bob, 10, and 4 and match the output below. I have written this code but it's not working num = 0 tot = 0 while True : sval = input('Enter a nunber:') if sval=='done' : break try: fval = float(sval) except : print('Invalid input') continue #print(fval) num = num+1 tot = tot+ fval #print('All done') print(tot,num,tot/num)

18th May 2020, 4:19 PM
prasad jagdale
prasad jagdale - avatar
5 Answers
18th May 2020, 4:52 PM
Abhay
Abhay - avatar
+ 3
You should include those lines: num = num + 1 tot = tot + fval within the while loop -- just increase the indentation by one step.
18th May 2020, 4:30 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 1
You aren't checking for smaller and larger numbers ,just adding them ,so you need to keep num and tot inside while loop num = 0 tot = 0 while True : sval = input('Enter a nunber:') if sval=='done' : break try: fval = float(sval) except : print('Invalid input') continue print(fval) num = num+1 tot = tot+ fval print('All done') print(tot,num,tot/num)
18th May 2020, 4:35 PM
Abhay
Abhay - avatar
0
max= 0 min= 0 while True : sval = input('Enter a nunber:') if sval=='done' : break try: fval = float(sval) except : print('Invalid input') continue #print(fval) fval = float(sval) # check for max value if max<fval max= fval # check for min value if min is none min= fval elif fval < min min= fval #print('All done') print('Maximum is ',max) print('Minimun is ',min) In this code I am getting error in line 15 kindly help me
18th May 2020, 5:34 PM
prasad jagdale
prasad jagdale - avatar
0
You are missing these ":" also not proper indentation
18th May 2020, 5:41 PM
Abhay
Abhay - avatar