鱼C论坛

 找回密码
 立即注册
查看: 1314|回复: 3

[已解决]列表打印

[复制链接]
发表于 2021-10-21 22:33:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#  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[i] * 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()

想问一下列表中的内容怎么连续打印看不出列表的中括号和引号,参考这个程序的使用
最佳答案
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 = ' , ')
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 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 = ' , ')
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-22 00:05:02 | 显示全部楼层
jackz007 发表于 2021-10-21 23:06
这一句:

        改为:

TypeError: not all arguments converted during string formatting
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-22 00:38:15 From FishC Mobile | 显示全部楼层
1005663861 发表于 2021-10-22 00:05
TypeError: not all arguments converted during string formatting

        用没修改的程序给出一个输入输出的样例看看呢。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-1-12 23:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表