Created
June 9, 2018 16:28
-
-
Save Mohamed2del/0baeed31fdd8208f0b99e73adcfa1884 to your computer and use it in GitHub Desktop.
Write a program which repeatedly reads numbers until the user enters "done". Once "done" is entered, print out the total, count, and average of the numbers. If the user enters anything other than a number, detect their mistake using try and except and print an error message and skip to the next number.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
num = 0 | |
tot = 0.0 | |
while True: | |
number = input("Enter a number") | |
if number == 'done': | |
break | |
try : | |
num1 = float(number) | |
except: | |
print('Invailed Input') | |
continue | |
num = num+1 | |
tot = tot + num1 | |
print ('all done') | |
print (tot,num,tot/num) |
I started learning to code 2 days ago so my code is probably written in the worst way possible so dont use it i just want to share it :
total = 0
avr = None
count = 0
while True:
answer = input('Enter a number: ')
if answer == 'done' : print(total, count, float(avr)), quit()
try: int(answer)
except:
print('Invalid Input')
continue
answers = [answer]
for number in answers :
count = count + 1
total = total + int(answer)
avr = total / count
answer2 = input('Enter a number: ')
if answer2 == 'done' :
print(total, count, float(avr)), quit()
try: int(answer2)
except:
print('Invalid Input')
continue
answers = [answer2]
for number in answers :
count = count + 1
total = total + int(answer2)
avr = total / count
answers = [answer,answer2]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
try to under while loop , its very simple if you understand