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
推荐的帖子
Read input as an integer in Python
在Python中将输入读取为整数
Read input as a float in Python
在Python中以浮点形式读取输入
Parse a string to float in Python (float() function)
解析要在Python中浮动的字符串(float()函数)
How do you read from stdin in Python?
您如何从Python的stdin中读取信息?
Asking the user for integer input in Python | Limit the user to input only integer value
要求用户在Python中输入整数| 限制用户仅输入整数值
Asking the user for input until a valid response in Python
要求用户输入直到Python中的有效响应
Input a number in hexadecimal format in Python
在Python中以十六进制格式输入数字
Input a number in binary format in Python
在Python中以二进制格式输入数字
How to get the hexadecimal value of a float number in python?
如何在python中获取浮点数的十六进制值?
Convert an integer value to the string using str() function in Python
使用Python中的str()函数将整数值转换为字符串
Convert a float value to the string using str() function in Python
使用Python中的str()函数将浮点值转换为字符串
Input and Output Operations with Examples in Python
使用Python中的示例进行输入和输出操作
Taking multiple inputs from the user using split() method in Python
使用Python中的split()方法从用户获取多个输入
Fast input / output for competitive programming in Python
快速输入/输出,可在Python中进行有竞争力的编程
Precision handling in Python
Python中的精确处理
Python print() function with end parameter
带有结束参数的Python print()函数
翻译自: https://www.includehelp.com/python/input-a-number-in-octal-format.aspx
python中八进制