Using try and except statement for exception handling, using raise to throw out the exception and stop the program.
The for loop repeats codes within itself for a fixed number of times determined by the specified expression.
An if else statement is a conditional statement that runs a different set of statements depending on whether an expression is true or false.
Method 1
The def keyword defines a function. A function is a group of statements that can be called upon repeatedly to perform a specific task.
Write a code or draw a flowchart to express a quadratic expression in factorised form or indicate that it cannot be factorised. Use inputs b and c , where b and c are positive integers.
The print built-in function displays the specified string to the output.
X
Method 2
Using try and except statement for exception handling, using raise to throw out the exception and stop the program.
Game Code
00:00
A variable stores a value that can be referred to or modified later in the program. A name, output, is assigned to the variable as an identifier in this case.
def load_input(input_str):
try:
x = int(input(input_str))
if x<0:
raise ValueError
else:
return x
except ValueError:
print("Enter positive whole number only!")
raise
b = load_input('Enter the value of b: ')
c = load_input('Enter the value of c: ')
output = ""
for i in range(1,c+1):
if c % i ==0:
j = c // i
if i + j == b:
output = "The factorised form is (x + {c1})(x + {c2})".format(c1=i,c2=j)
break
if output == "":
output = "The expression cannot be factorised."
print(output)
def load_input(input_str):
try:
x = int(input(input_str))
if x<0:
raise ValueError
else:
return x
except ValueError:
print("Enter positive whole number only!")
raise
b = load_input('Enter the value of b: ')
c = load_input('Enter the value of c: ')
output = ""
for i in range(b):
j = b - i
if i*j == c:
output = "The factorised form is (x + {c1})(x + {c2}).".format(c1=i,c2=j)
break
if output == "":
output = "The expression cannot be factorised."
print(output)