1005663861 发表于 2021-10-21 22:33:44

列表打印

#binary to decimal
def bin2int(binary_number):
    number = len(binary_number) - 1
    decimal_number = 0

    for i in binary_number:
      i = int(i)
      decimal_number += i * 2 ** (i * number)
      number -= 1
    return decimal_number


#decimal to binary
def int2bin(decimal_number):
    binary = []

    while decimal_number != 0:
      binary_number = decimal_number % 2
      decimal_number //= 2
      binary_number = str(binary_number)
      binary.append(binary_number)

    binary.reverse()
    binary = list(map(int, binary))
    return binary


#octal number to decimal
def oct2int(octal_number):
    number = len(octal_number) - 1
    decimal_number = 0

    for i in octal_number:
      i = int(i)
      decimal_number += i * 8 ** (i * number)
      number -= 1
    return decimal_number


#decimal to octal
def int2oct(decimal_number):
    octal_number = []

    while decimal_number != 0:
      oct_number = decimal_number % 8
      decimal_number //= 8
      oct_number = str(oct_number)
      octal_number.append(oct_number)

    octal_number.reverse()
    octal_number = list(map(int, octal_number))
    return octal_number


#hexadecimal to decimal
def hex2int(hex_number):
    length = len(hex_number)
    hex_number = hex_number.upper()
    decimal = 0
    hex_accord = {'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15}
    n = 0

    for i in hex_number:
      n += 1
      if '0' <= i <= '9':
            i = int(i)
            decimal += i * 16 ** (length - n)
      else:
            decimal += hex_accord * 16 ** (length - n)

    return decimal


#decimal to hexadecimal
def int2hex(decimal_number):
    hexadecimal = ''
    if 0 <= decimal_number <= 9:
      hexadecimal = str(decimal_number)
    elif decimal_number == 10:
      hexadecimal = 'A'
    elif decimal_number == 11:
      hexadecimal = 'B'
    elif decimal_number == 12:
      hexadecimal = 'C'
    elif decimal_number == 13:
      hexadecimal = 'D'
    elif decimal_number == 14:
      hexadecimal = 'E'
    elif decimal_number == 15:
      hexadecimal = 'F'

    return hexadecimal


def int2hexadecimal(decimal_number):
    hexadecimal_number = []

    while decimal_number != 0:
      hex_number = decimal_number % 16
      decimal_number //= 16
      hex_number = int2hex(hex_number)
      hexadecimal_number.append(hex_number)

    hexadecimal_number.reverse()
    return hexadecimal_number


def transmission(execution_start, execution_aim, number):
    if execution_start == 2:
      number = bin2int(number)
    elif execution_start == 8:
      number = oct2int(number)
    elif execution_start == 10:
      number = number
    elif execution_start == 16:
      number = hex2int(number)
    else:
      print('please enter a true system!')
      return False

    if execution_aim == 2:
      number = int2bin(number)
    elif execution_aim == 8:
      number = int2oct(number)
    elif execution_aim == 10:
      number = number
    elif execution_aim == 16:
      number = int2hexadecimal(number)
    else:
      return False
    return number


def main():
    number = str(input('please input a number:'))
    execution_start = int(input('Which system you want to start:'))
    execution_aim = int(input('Which system you want to reach:'))
    if transmission(execution_start, execution_aim, number):
      print('The number you want to transfer is:', transmission(execution_start, execution_aim, number), sep='')
    else:
      print('please enter a true system!')


if __name__ == '__main__':
    main()


想问一下列表中的内容怎么连续打印看不出列表的中括号和引号,参考这个程序的使用

jackz007 发表于 2021-10-21 23:06:39

本帖最后由 jackz007 于 2021-10-21 23:10 编辑

      这一句:
      print('The number you want to transfer is:', transmission(execution_start, execution_aim, number), sep='')
      改为:
      print('The number you want to transfer is : ' , end = ' ')
      print(* transmission(execution_start, execution_aim, number) , sep = ' , ')

1005663861 发表于 2021-10-22 00:05:02

jackz007 发表于 2021-10-21 23:06
这一句:

      改为:

TypeError: not all arguments converted during string formatting

jackz007 发表于 2021-10-22 00:38:15

1005663861 发表于 2021-10-22 00:05
TypeError: not all arguments converted during string formatting

      用没修改的程序给出一个输入输出的样例看看呢。
页: [1]
查看完整版本: 列表打印