python十进制转八进制_如何使用Python将八进制转换为十进制

也许有人会觉得这些有用

这些第一行接受任何十进制数并将其转换为任何所需的基数def dec2base():

a= int(input('Enter decimal number: \t'))

d= int(input('Enter expected base: \t'))

b = ""

while a != 0:

x = '0123456789ABCDEF'

c = a % d

c1 = x[c]

b = str(c1) + b

a = int(a // d)

return (b)

第二行也一样,但是对于给定的范围和给定的小数点def dec2base_R():

a= int(input('Enter start decimal number:\t'))

e= int(input('Enter end decimal number:\t'))

d= int(input('Enter expected base:\t'))

for i in range (a, e):

b = ""

while i != 0:

x = '0123456789ABCDEF'

c = i % d

c1 = x[c]

b = str(c1) + b

i = int(i // d)

return (b)

第三行从任何基数转换回小数def todec():

c = int(input('Enter base of the number to convert to decimal:\t'))

a = (input('Then enter the number:\t ')).upper()

b = list(a)

s = 0

x = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']

for pos, digit in enumerate(b[-1::-1]):

y = x.index(digit)

if int(y)/c >= 1:

print('Invalid input!!!')

break

s = (int(y) * (c**pos)) + s

return (s)

注意:如果有人需要,我也有GUI版本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值