Skip to content

Instantly share code, notes, and snippets.

@Mohamed2del
Created June 9, 2018 16:28
Show Gist options
  • Save Mohamed2del/0baeed31fdd8208f0b99e73adcfa1884 to your computer and use it in GitHub Desktop.
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.
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)
@Hazengard
Copy link

My code:

`count = 0
s = 0

while True:
x = input('Enter a number:')
if x == 'Done':
break
try:
y = float(x)
s = s + y
count = count + 1
average = s / count
except:
print('Invalid input.')

print('End of the process.')
print('Number of numbers entered:', count)
print('Average:', round(average, 2))
print('Total:', s)`

@kajoll0304
Copy link

kajoll0304 commented Dec 13, 2021

Please can you explain in simple manner that a 1st year 1st sem could understand?

@kholoud24
Copy link

count = 0
total = 0
average = 0
while True:
input_value = input('enter number\n')
if input_value == "done":
break
try:
input_value = int(input_value)
count = count+1
total = total+input_value
except:
print('invalid data')
if count:
average = total/count
print(count, total, average)

@imranlondon
Copy link

imranlondon commented Sep 19, 2022

The New value of the variable depends on the old. This is why initialise both.

count = 0
total = 0

loop use to take more then one value from user

while True:
nu = input("Enter a Number :")

use condition statement to stop the loop

if nu == "done":
    break

try and except we use to catch wrong input

try:
    number = int(nu)
except:
    print("Invalid input")

after catching wrong input we want , user enter the right input again

    continue
print(number)

count will count , how many times loop with run

count = count + 1

Total will add all the value one by one

total = total + number

final statement to rum when loop end

print(total, count, (total/count))

@imranlondon
Copy link

Write a program which repeatedly reads numbers until the user enters the repeated number. Please tell friends how can i do this code with simple python programming.

try to under while loop , its very simple if you understand

@Stan408
Copy link

Stan408 commented Dec 27, 2022

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