帮助将Int转换为二进制Python 3而不使用(“bin()”) [英] help to convert Int to binary Python 3 without using("bin()")
本文介绍了帮助将Int转换为二进制Python 3而不使用(“bin()”)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我很擅长编程所以我来这里为你专业人士帮助我摆脱,如何制作一个程序转换整数(从用户输入)到二进制代码,不使用bin()函数。
这是我自己开始做的事情:
Hi,
I'm pretty new to programation so I came here for you "pros" to help me fidding out, how to make a program that convert integer(from the user input) to a binary code, without using the bin() function.
Here is what I started to do by myself:
nDecimal = eval(input("entrer un décimal positif :"))
print("Le nombre décimal entrer est:", nDecimal)
#transformation into binairy:
while nDecimal != 1 or 0:
for i in nDecimal:
i = nDecimal % len(nDecimal)
if i > 1 or 0:
i += 1
else:
print("votre nombre", nDecimal, "en binaire est:", i)
break
当然代码不起作用:(。我试图使用用户输入的模数并添加到它1直到 nDecimal不再可分割。
Ps:对不起我的英语。
Of course that code doesn't work :(. I tried to use the modulo of the user input and add to it 1 until the "nDecimal" is not divisible anymore.
Ps: Sorry for my sh*tty english.
推荐答案
要将十进制数转换为二进制数,您需要将剩余的连续除以2,然后反向打印。例如,数字13是
To convert a decimal number to its binary equivalent you need to take the remainder of successively dividing by 2, and print in reverse. For example the number 13 is
13 / 2 = 6 rem 1
6 / 2 = 3 rem 0
3 / 2 = 1 rem 1
1 / 2 = 0 rem 1
binary = 1101
尝试
Try
nDecimal = eval(input("entrer un décimal positif :"))
print("Le nombre décimal entrer est:", nDecimal)
nbin=[]
while nDecimal > 0:
value = int(nDecimal % 2)
# print (value)
nDecimal = int(nDecimal / 2)
nbin.append(value)
nbin.reverse()
print("Le nombre binaire est", end=": ")
for x in nbin:
print(x, end='')
这篇关于帮助将Int转换为二进制Python 3而不使用(“bin()”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文