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 hidden or 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) |
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))
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
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
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)