[0] The built-in functions can be consulted by Help or https://docs.python.org/3/library/.
[1] for the conversion of the binary, the 10 binary integer to 2,8,16,bin (), Oct (), Hex () can be converted to the corresponding system.
[2] The conversion of each system into 10, int (obxxxxx), int (ox ...) or int (' xxxxxxxxxx ', 2), int (' xxxxxxxx ', 8)
The following code is used as an exercise to solve any conversion of M-feed to n -ary. As a good habit, we should check all possible inputs, m,n must be integers, the number to be processed should also be integers (but not necessarily positive numbers), m if not number, calling int would be wrong, How can I tell if a string is a number? You need to use regular expressions here and then process them later.
m = Int (input (' please input the orginal number system ')) n = Int (input (' please input the Objectiv E number system ') M_str = input (' The orginal Integer ') if (float (m_str) ==int (float (m_str)): m_number = Int (m_st
R,M) #get The numeric value of M symbol = ' if (m_number<0): #figure out the final symbol
M_number = ABS (M_number) #get positive value symbol = '-' if (n = =): # m convert to 10 Print (Symbol+str (m_number)) Else: #10 convert to n ans = "while (M_number) : ans = str (m_number%n) + ans#doing the MoD m_number = m_number//n#warning !!: Using//Not/print (Symbol+ans) #print the answer else:print (' Input is not integer ')
====================================================================================== Society defines functions: def:, return value ( Multiple return values are actually a tuple), parameter checking. The code for solving the solution of a two-time equation.
Import Math
def quadratic (A, B, c):
if (not Isinstance (A, (int,float)) and isinstance (b, (int,float)) and Isinstance (c, (Int,float))): #check The argument type
print (' The a,b,c is Typerror ') return
if (b*b>=4* A*C):
r = math.sqrt (b*b-4*a*c) return
(-b+r)/(2*a), (-b-r)/(2*a)
else:
print (' There isn't real root ')
return
print (quadratic (2, 3, 1)) print (
quadratic (1, 3,-4))
print (quadratic (1,-2, 1
)) Print (Quadratic (5, 1, 3))
print (Quadratic (' 1 ', 3,-4))