Python Forum
Is there a specialist in error handling ?
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is there a specialist in error handling ?
#1
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
Reply
#2
When you paste from your IDE use CTRL+SHIFT+V instead of CTRL+V to paste the code as plain text without formatting.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
What have you tried? This shouldn't require a specialist, its (probably) basic error handling and you need to elaborate further on the requirements.
Reply
#4
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
Reply
#5
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.')
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
"Unable" could mean disallowed or failing after trying.
Reply
#7
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
Reply
#8
(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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#10
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Handling IO Error / Reading from file Expel 10 4,712 Jul-18-2019, 01:21 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020