>>> oct(83)
0o123
文中涉及代码:
def DtoO(num):
if num>0:
DtoO(num//8)
print(num%8, end='')
num=int(input('请输入一个十进制数字:'))
print("八进制:",end="")
DtoO(num)
print()
def DtoO(num):
o,i=0,1
while num!=0:
y=num%8
o =y*i
i=i*10
num//=8
print(o)
num=int(input('请输入一个十进制数字:'))
print("八进制:",end="")
DtoO(num)
print()
def DtoO(num):
o=[0]*100
i=0
while num!=0:
o[i]=num%8
num=num//8
i =1
for j in range(i - 1, -1, -1):
print(o[j], end="")
num=int(input('请输入一个十进制数字:'))
print("八进制:",end="")
DtoO(num)
print()
def OtoD(num):
if num==0:
return 0
else:
return (num) 8*OtoD(num// 10)
num=int(input('请输入一个八进制数字:'))
print(f"十进制:{OtoD(num)}")
def OtoD(num):
d=0
length=len(num)
for x in num:
length=length-1
d =pow(8,length)*int(x)
return d
num=input('请输入一个八进制数字:')
print(f"十进制:{OtoD(num)}")
def OtoD(num):
d=0
base=1
while num>0:
n=num
num=num//10
d =n*base
base=base*8
return d
num=int(input('请输入一个八进制数字:'))
print(f"十进制:{OtoD(num)}")
文章创作不易,如果您喜欢这篇文章,请关注、点赞并分享给朋友。如有意见和建议,请在评论中反馈!