仅转换列表中的特定项目

问题描述 投票:0回答:0

我正在尝试编写一个代码,将一串数字转换为浮点数,以便将它们转换为八进制和 ascii 值。但是,如果数字不能转换为八进制(又名负数),则应将其打印为负浮点数。目前我可以把东西变成 ascii 值,但它仍然不会跳过负值。

characters = input("Input a string of numbers to be encrypted with spaces in between each value: ")
charcters = str(characters)

values = characters.split()
new_values = [float(i) for i in values]
print(new_values)

integers = list(map(int, new_values))

octal_values = list(map(oct, integers))
print(octal_values)
ascii_values = list(map(chr, octal_values))
print(ascii_values)

这是我尝试分离负浮点数和正浮点数的方法(显然行不通):

characters = input("Input a string of numbers to be encrypted with spaces in between each value: ")
charcters = str(characters)

values = characters.split()
new_values = [float(i) for i in values]
print(new_values)

integers = list(map(int, new_values))

for num in integers:
    if num < 0:
        print(end= " ")
    if num > 0:
        octal_values = list(map(oct, num))
        print(octal_values)
        ascii_values = list(map(chr, octal_values))
        print(ascii_values)
python-3.x list encryption
© www.soinside.com 2019 - 2024. All rights reserved.