python中八进制_在Python中以八进制格式输入数字

python中八进制

Syntax to convert octal value to an integer (decimal format),

将八进制值转换为整数(十进制格式)的语法,

    int(oct_value, 8)

Here,

这里,

  • oct_value should contain the valid octal value

    oct_value应该包含有效的八进制值

  • 8 is the base value of the octal number system

    8是八进制数的基值

Note: oct_value must contain only octal digits (0, 1, 2, 3 ,4 ,5 ,6, 7), if it contains other than these digits a "ValueError" will return.

注意oct_value必须仅包含八进制数字(0、1、2、3、4、5、6、7),如果它不包含这些数字,则将返回“ ValueError”

程序将给定的八进制值转换为整数(十进制) (Program to convert given octal value to integer (decimal))

# function to convert given octal Value
# to an integer (decimal number)
def OctToDec(value):
  try:
    return int(value, 8)
  except ValueError:
    return "Invalid Octal Value"

# Main code
input1 = "1234567"
input2 = "7001236"
input3 = "1278"

print(input1, "as decimal: ", OctToDec(input1))
print(input2, "as decimal: ", OctToDec(input2))
print(input3, "as decimal: ", OctToDec(input3))

Output

输出量

1234567 as decimal:  342391
7001236 as decimal:  1835678
1278 as decimal:  Invalid Octal Value

Now, we are going to implement the program – that will take input the number as an octal number and printing it in the decimal format.

现在,我们将实现该程序–将输入的数字作为八进制数字并以十进制格式打印。

程序以八进制格式输入数字 (Program to input a number in octal format)

# input number in octal format and 
# converting it into decimal format

try:
  num = int(input("Input octal value: "), 8)
  print("num (decimal format):", num)
  print("num (octal format):", oct(num))  
except ValueError:
  print("Please input only octal value...")

Output

输出量

RUN 1:
Input octal value: 1234567
num (decimal format): 342391
num (octal format): 0o1234567

RUN 2:
Input octal value: 12700546
num (decimal format): 2851174
num (octal format): 0o12700546

RUN 3:
Input octal value: 12807
Please input only octal value...

Recommended posts

推荐的帖子

翻译自: https://www.includehelp.com/python/input-a-number-in-octal-format.aspx

python中八进制

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值