【问题标题】:Error with calculating of octal system in Python在 Python 中计算八进制系统时出错
【发布时间】:2020-03-29 12:25:18
【问题描述】:

我只是检查例如 1 + 2 是否等于 3,所以它在二进制代码中表示 0011。

ox_number = 361
output = []

for num in ox_number.split():
  if 1 == num:
    output.append("001")
  elif 1 + 2 == num:
    output.append("011")
  else 1 + 2 + 4 == num:
    output.append("111")

connected = ''.join(output)
rever_ = reversed(connected)

print(rever_)

【问题讨论】:

  • 可以直接将号码表示为{:o}.format(ox_number)

标签: python octal


【解决方案1】:

您可以使用

将整数与八进制格式的字符串相互转换
your_int = 386
its_octal_is = f"{your_int:o}"
# Equivalent to
its_octal_is = "{:o}".format(your_int)
int_from_octal_string = int("386",8)

您可能想查看int 文档以及格式表达式here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-09
    • 1970-01-01
    • 2013-09-19
    • 2019-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多