Doubt in small python code | SoloLearn: Learn to code for FREE!

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)

5/18/2020 4:19:05 PM

prasad jagdale

5 Answers

New Answer

+1

prasad jagdale All things r right https://code.sololearn.com/cTm2A1XHtvIs/?ref=app

+3

You should include those lines: num = num + 1 tot = tot + fval within the while loop -- just increase the indentation by one step.

+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)

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

0

You are missing these ":" also not proper indentation