-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexception.py
More file actions
84 lines (64 loc) · 1.84 KB
/
exception.py
File metadata and controls
84 lines (64 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# def factorial(n):
# # n! can also be defined as n * (n-1)!
# """ calculates n! recursively """
# if n <= 1:
# return 1
# else:
# print(n/0)
# return n * factorial(n-1)
#
# try:
# print(factorial(900))
# except (RecursionError, OverflowError):
# print("This program cannot calculate factorials that large")
# except ZeroDivisionError:
# print("Division by zero")
#
#
# print("Program terminating")
import sys
def getint(prompt):
while True:
try:
number = int(input(prompt))
return number
except ValueError:
print("Invalid number entered, please try again")
except EOFError:
sys.exit(1)
first_number = getint("Please enter first number ")
second_number = getint("Please enter second number ")
try:
print("{} divided by {} is {}".format(first_number, second_number, first_number / second_number))
except ZeroDivisionError:
print("You can't divide by zero")
sys.exit(2)
# define Python user-defined exceptions
class Error(Exception):
"""Base class for other exceptions"""
pass
class ValueTooSmallError(Error):
"""Raised when the input value is too small"""
pass
class ValueTooLargeError(Error):
"""Raised when the input value is too large"""
pass
# our main program
# user guesses a number until he/she gets it right
# you need to guess this number
number = 10
while True:
try:
i_num = int(input("Enter a number: "))
if i_num < number:
raise ValueTooSmallError
elif i_num > number:
raise ValueTooLargeError
break
except ValueTooSmallError:
print("This value is too small, try again!")
print()
except ValueTooLargeError:
print("This value is too large, try again!")
print()
print("Congratulations! You guessed it correctly.")