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)
@joannewangziyi
Copy link

Can anyone help me explain why I cannot run the code without adding "While true:" statement? I don't know why we have to add this line to "make the loop as infinite" so that it can run. Thanks!

Copy link

ghost commented Feb 9, 2020

can you explain the try statement please?

@sachin-web
Copy link

@joann

Can anyone help me explain why I cannot run the code without adding "While true:" statement? I don't know why we have to add this line to "make the loop as infinite" so that it can run. Thanks!

For loop can also be used but that will make this simple program much more complicated.
even though he used while loop, the keyword like break and continue makes his program control over the iteration process.

@mahesh12021999
Copy link

count = 0
total = 0
while True:
no = input("enter the guess: " )
if no == "done":
print("done!")
break

try :
    nu = int(no)
    print(nu)
except:
    print("invalid no")
    continue

count = count+1
total = total + int(nu)

print("the count is ",count)
print("total ",total)
avg = total/count
print("avg",avg)

@j4jeyaram
Copy link

count=0
total = 0
inp = 1
while True:
try:
number = input('Enter a number')
if number == 'done':
break
inp = int(number)
count = count + 1
total = total + inp
except:
print('Enter a valid number')
print(count, total/count)

https://gist.github.com/j4jeyaram/a9167e981d91e682f58613ba83a24d86

@vr047469
Copy link

Can anyone help me explain why I cannot run the code without adding "While true:" statement? I don't know why we have to add this line to "make the loop as infinite" so that it can run. Thanks!

while loop is used because, need to input like infinite until "done".

@j4jeyaram
Copy link

Since we are declaring the while condition true at the initial statement (without using any argument), it makes the program to flow through the series of lines without blowing up. Also we have structured the program till the user enters a non integer value "done". Hence, the program counts the number of entries made, as we have used an infinite loop.
Hope this clarifies....

@DevanshS157
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.

@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