八进制转十进制的数字转换

huangapple go评论130阅读模式
英文:

Octal to Decimal conversion of number

问题

import math

def octalToDecimal(octalNumber):
    decimalNumber = 0
    i = 0
    while (math.floor(octalNumber) != 0):
        rem = math.floor(octalNumber) % 10
        octalNumber /= 10
        decimalNumber += rem * 8**i
        ++i
    print(decimalNumber)

octalToDecimal(123) 
英文:

I want to convert an octal number into a decimal one. Why am I getting 6 as an output instead of 83?

import math

def octalToDecimal(octalNumber):
    decimalNumber = 0
    i = 0
    while (math.floor(octalNumber) != 0):
        rem = math.floor(octalNumber) % 10
        octalNumber /= 10
        decimalNumber += rem * 8**i
        ++i
    print(decimalNumber)

octalToDecimal(123) 

答案1

得分: 2

写成i += 1而不是++i,Python中没有++操作符。

英文:

write i+=1 instead of ++i,theres no such operator as ++ in python

huangapple
  • 本文由 发表于 2023年3月7日 01:06:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75653771.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定