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
推荐的帖子
Asking the user for integer input in Python | Limit the user to input only integer value
How to get the hexadecimal value of a float number in python?
Convert an integer value to the string using str() function in Python
Convert a float value to the string using str() function in Python
Taking multiple inputs from the user using split() method in Python
翻译自: https://www.includehelp.com/python/input-a-number-in-octal-format.aspx
python中八进制