Second order determinant in Python

Title Description

Input 4 values of the second-order determinant, and calculate the value of the second-order determinant.

 

I / O description

Enter four values for the second-order determinant as follows:

Please input a1 value: 5
Please input a2 value: 9
Please input b1 value: 3
Please input b2 value: 4

Output the second-order determinant and print its value. As follows:

second order determinant: 
|    5    3    |
|    9    4    |
Value is : -7

 

Solutions

Input four values of the second-order determinant in turn, and evaluate according to the operation rules of the second-order determinant. Finally, print out the results.

 

Code

import traceback


array = [[None] * 2 for row in range(2)]

try:
    array[0][0] = int(input("Please input a1 value: "))
    array[1][0] = int(input("Please input a2 value: "))
    array[0][1] = int(input("Please input b1 value: "))
    array[1][1] = int(input("Please input b2 value: "))

    value = array[0][0] * array[1][1] - array[1][0] * array[0][1]
    print("second order determinant: ")
    print("|\t%s\t%s\t|" % (array[0][0], array[0][1]))
    print("|\t%s\t%s\t|" % (array[1][0], array[1][1]))
    print("Value is : %s" % value)
except ValueError as e:
    print("Invalid number input.")
    print(str(e))
    print("errmsg: \n%s" % traceback.format_exc())
except Exception as e:
    print(str(e))
    print("errmsg: \n%s" % traceback.format_exc())

 

Code review

import traceback

# Declare a two-dimensional list to represent an empty second-order determinant
array = [[None] * 2 for row in range(2)]

try:
    # Enter four values of the second-order determinant
    array[0][0] = int(input("Please input a1 value: "))
    array[1][0] = int(input("Please input a2 value: "))
    array[0][1] = int(input("Please input b1 value: "))
    array[1][1] = int(input("Please input b2 value: "))

    # Calculation results according to the evaluation rules of the second-order determinant
    value = array[0][0] * array[1][1] - array[1][0] * array[0][1]

    # Print results
    print("second order determinant: ")
    print("|\t%s\t%s\t|" % (array[0][0], array[0][1]))
    print("|\t%s\t%s\t|" % (array[1][0], array[1][1]))
    print("Value is : %s" % value)

# If the input second-order determinant value is not data that can be converted to integer, the exception thrown will be caught here. Prints error prompts and stack information.
except ValueError as e:
    print("Invalid number input.")
    print(str(e))
    print("errmsg: \n%s" % traceback.format_exc())
# Catch exceptions that may be thrown elsewhere and print error messages and stacks
except Exception as e:
    print(str(e))
    print("errmsg: \n%s" % traceback.format_exc())

 

Portal

1. range() function

https://blog.csdn.net/TCatTime/article/details/82941022

2. input() function

https://blog.csdn.net/TCatTime/article/details/82556033

3. int() function

https://blog.csdn.net/TCatTime/article/details/82826824

4. print() function

https://blog.csdn.net/TCatTime/article/details/83450692

5. str() function

https://blog.csdn.net/TCatTime/article/details/82963437

 

test case

1. Input data are all integer

Please input a1 value: 12
Please input a2 value: 34
Please input b1 value: 1
Please input b2 value: 78
second order determinant: 
|	12	1	|
|	34	78	|
Value is : 902

 

2. There are illegal characters in the input data

When the input data is not an integer, such as a string.

Please input a1 value: 3
Please input a2 value: 34
Please input b1 value: 12
Please input b2 value: f2
Invalid number input.
invalid literal for int() with base 10: 'f2'
errmsg: 
Traceback (most recent call last):
  File "/Users/Desktop/Python Apps/untitled_test/test3.py", line 10, in <module>
    array[1][1] = int(input("Please input b2 value: "))
ValueError: invalid literal for int() with base 10: 'f2'

The program will give an error prompt and print the stack information.

Tags: Python

Posted on Fri, 08 Nov 2019 11:11:48 -0500 by turboprop