-
-
Save Mohamed2del/0baeed31fdd8208f0b99e73adcfa1884 to your computer and use it in GitHub Desktop.
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) |
can you explain the try statement please?
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.
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)
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
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".
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....
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.
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)`
Please can you explain in simple manner that a 1st year 1st sem could understand?
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)
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]
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!