python小程序

来源:互联网 发布:淘宝评价管理系统 编辑:程序博客网 时间:2024/06/14 01:19

python小程序

1.10进制转任意进制:
def decimalToNBaseByNormal(decimalVar, base):
tempList = []
temp = decimalVar
i = 0
while (temp > 0):
ord = temp % base
if (ord > 9): #如果余数大于9,则以字母的形式表示
ord = chr(65 + (ord - 10)) #把数字转换成字符
tempList.append(ord)
temp = int(temp / base)
i = i + 1
tempList.reverse();
#print(tempList)
binary = “”
for j in range(len(tempList)):
binary = binary + str(tempList[j]);
print(“the decimal is: %d and after convering by %d base is %s”%(decimalVar, base, binary))
st = decimalToNBaseByNormal(-41,3)

原创粉丝点击