Posts: 382
Threads: 94
Joined: Mar 2017
May-21-2017, 07:48 PM
(This post was last modified: May-21-2017, 10:53 PM by snippsat.)
my question here:Please help me to finish with this file. I am a beginner and I am unable to use "try" and "except"
# file2.py
size = 40
print("here is the addition of many numbers (even with a .) Maximum of numbers=", size)
print("Once you have finished your list of numbers, just press $ to indicate the end\n")
total = 0.0
value = 0.0
count = 1
while (count != 0):
value = float(input("enter the value: "))
if value == '$':
average = total / count
print("average value= ", average)
exit()
else:
print(value)
print(total)
total = value + total
print(total)
count = count + 1
Posts: 2,953
Threads: 48
Joined: Sep 2016
May-21-2017, 07:48 PM
(This post was last modified: May-22-2017, 05:10 AM by wavic.)
When you paste from your IDE use CTRL+SHIFT+V instead of CTRL+V to paste the code as plain text without formatting.
Posts: 2,342
Threads: 62
Joined: Sep 2016
What have you tried? This shouldn't require a specialist, its (probably) basic error handling and you need to elaborate further on the requirements.
Posts: 7,320
Threads: 123
Joined: Sep 2016
May-21-2017, 11:06 PM
(This post was last modified: May-21-2017, 11:06 PM by snippsat.)
Here a hint:
>>> total = 10
>>> count = 5
>>> average = total / count
>>> average
2.0
>>> count = 0
>>> average = total / count
Traceback (most recent call last):
File "<string>", line 301, in runcode
File "<interactive input>", line 1, in <module>
ZeroDivisionError: division by zero
>>> # Now using try:except to catch that error
>>> try:
... average = total / count
... except ZeroDivisionError:
... print('Cannot divide by zero,try again')
...
Cannot divide by zero,try again
Posts: 4,220
Threads: 97
Joined: Sep 2016
He said he can't use try and except. In that case, you need to do defensive programming:
if count:
average = total / count
print('The average is', average)
else:
print('Cannot divide by zero, try again.')
Posts: 2,342
Threads: 62
Joined: Sep 2016
"Unable" could mean disallowed or failing after trying.
Posts: 382
Threads: 94
Joined: Mar 2017
May-22-2017, 05:14 AM
(This post was last modified: May-22-2017, 10:00 AM by snippsat.)
my question here:Hi all. I should be very glad if you help me using error handling
# file2.py
size = 40
print("here is the addition of many numbers (even with a .) Maximum of numbers=", size)
print("Once you have finished your list of numbers, just press $ to indicate the end\n")
total = 0.0
value = 0.0
count = 1
while (count != 0):
value = float(input("enter the value: "))
if value == '$':
average = total / count
print("average value= ", average)
exit()
else:
print(value)
print(total)
total = value + total
print(total)
count = count + 1 User has been warned for this post. Reason: 5
Posts: 2,953
Threads: 48
Joined: Sep 2016
(May-21-2017, 07:48 PM)wavic Wrote: When you paste from your IDE use CTRL+SHIFT+V instead of CTRL+V to paste the code as plain text without formatting.
Posts: 4,220
Threads: 97
Joined: Sep 2016
You seem to have posted the same question after getting two answers. If neither of those errors is helpful, you need to clarify your question.
Posts: 2,342
Threads: 62
Joined: Sep 2016
(May-22-2017, 05:14 AM)sylas Wrote: my question here:Hi all. I should be very glad if you help me using error handling (May-21-2017, 07:59 PM)micseydel Wrote: What have you tried? You didn't ask a question, and you didn't follow instructions. You need to elaborate much more on your requirements. You were given a very good hint, if that doesn't totally clarify things then say why.
|