Is there a specialist in error handling ? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Is there a specialist in error handling ? (/thread-3412.html) |
Is there a specialist in error handling ? - sylas - May-21-2017 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 RE: Is there a specialist in error handling ? - wavic - May-21-2017 When you paste from your IDE use CTRL+SHIFT+V instead of CTRL+V to paste the code as plain text without formatting. RE: Is there a specialist in error handling ? - micseydel - May-21-2017 What have you tried? This shouldn't require a specialist, its (probably) basic error handling and you need to elaborate further on the requirements. RE: Is there a specialist in error handling ? - snippsat - May-21-2017 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 RE: Is there a specialist in error handling ? - ichabod801 - May-22-2017 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.') RE: Is there a specialist in error handling ? - micseydel - May-22-2017 "Unable" could mean disallowed or failing after trying. Last endeavor for error handling - sylas - May-22-2017 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 RE: Is there a specialist in error handling ? - wavic - May-22-2017 (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. RE: Is there a specialist in error handling ? - ichabod801 - May-22-2017 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. RE: Is there a specialist in error handling ? - micseydel - May-22-2017 (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. |