ask:
[1] basic questions: a weight transducer design, the input to "g" after the return of the number of units in terms of "kg" Results
[2] basis title: Design of a rectification function of the hypotenuse, such as right-angle side length of 3 and 4, respectively, as output: The right triangle third side's length is 5.0
[Title] increase: Triangle print out (printed out to achieve a corresponding number of rows as the digital keyboard input, such as input 10, print the line 10)
answer:
method 1:
def weight_conversion(weight, option): if option == '1': return str(weight * 1000) + 'g' elif option == '2': return str(weight / 1000) + 'kg' else: print('没有此选项') if __name__ == '__main__': option = input('1. kg to g\n2. g to kg\n请选择:' ) Weight = int (INPUT ( ' Enter weight: ' )) Print (weight_conversion (weight, Option))
Method 2:
int = WG (INPUT ( " Please enter the base in grams (G) !!! \ n- " )) WKG = WG / 1000 Print ( " into kilogram:% F (kg) " % WKG)
[2] basis title: Design of a rectification function of the hypotenuse, such as right-angle side length of 3 and 4, respectively, as output: The right triangle third side's length is 5.0
method 1:
Import Math DEF hypotenuse (A, B): '' ' : A param: cathetus : param b: cathetus : return: hypotenuse ' '' return Math.sqrt (A ** 2 ** 2 + B ) IF the __name__ == ' __main__ ' : A = int (iNPUT ( " Please enter a right-angle side: " )) B = int (iNPUT ( " Please enter the other angle side: " )) Print ( " of The right Triangle Side length apos IS THIRD " , hypotenuse (A, B))
Method 2:
Import Math Print ( " .. " * 16 ) X = int (INPUT ( " Please enter the length of a side edge of the right triangle: \ n- " )) Y = int (INPUT ( " Please enter the right-angle side of the other side length: \ n- " )) Z = Math.sqrt (Y * X * X + Y) Print ( " .. " * 16 ) Print ( " hypotenuse length: {} " .format (Z ))
[Title] increase: Triangle print out (printed out to achieve a corresponding number of rows as the digital keyboard input, such as input 10, print the line 10)
method 1:
int = Lines (INPUT ( " Please enter the number of printed lines: " )) out_list, process_list = [. 1], [. 1 ] IF Lines. 1 == : Print ( ' {} ' .format ( ' ' .join (List (Map (STR, out_list))))) the else : for J in Range (lines): # of rows Print ( ' {} ' .format ( ' ' .join (List (Map (STR, out_list))))) for I in Range (. 1, J + 2): # next line number of each line if i > len(out_list) - 1: process_list.append(1) else: process_list.append(out_list[i - 1] + out_list[i]) out_list = process_list[:] process_list = [1]
Method 2:
import numpy as np def get_triangle(): row = np.array([1]) yield row margin_array = np.array([0]) while True: next_row = np.concatenate((margin_array, row)) + np.concatenate((row, margin_array)) yield list(next_row) row = next_row triangle = get_triangle() for i in range(10): print(f"row {i+1}: ", triangle.__next__())