如何简便的写一个十进制转化二进制
while 1:#仅支持100以下的换算variable = input('请输入10进制数字:')
# 让用户输入一个要转化为2进制的数字
before = int(variable)
# 将用户输入数字转化为整型
after = before // 2
# 用户输入数字10 返回 10//2=5 用于下次计算不显示
afters = before % 2
# 用户输入数字10 返回 10%2=0 用于显示
tis = after // 2
# 5//2=2 用于下次计算
tiss = after % 2
# 5%2=1 用于显示
pis = tis // 2
piss = tis % 2
tiis = pis // 2
tiiss = pis % 2
wins = tiis // 2
winss = tiis % 2
pos = wins // 2
poss = wins % 2
wier = pos // 2
wies = pos % 2
print(wies,poss,winss,tiss,piss,tiss,afters)
麻烦大佬们提供下思路 https://img-blog.csdn.net/20160720140605151 直接有bin(),但结果前会有“0b”
例如 bin(3)
结果为0b11 c=int(input(N:))
print(bin(c))
欢迎追问 青出于蓝 发表于 2021-7-11 17:18
欢迎追问
while 1:
print('仅支持100以下换算')
variable = input('请输入10进制数字:')
# 用户输入一个数字 2
before = int(variable)
# 讲用户输入数字转化整型
after = before // 2
if before == 2:
afters = before % 2
print(after,afters)
else:
afters = before % 2
if before == 1:
print('1')
else:
if before == 0:
print('0')
else:
if before < 0:
print('请输入一个比0大的数字!')
if before > 100:
print('请输入100以下的数字!')
else:
tis = after // 2
tiss = after % 2
if after == 1:
tiss = 1
print(tiss,afters)
else:
tiss = after % 2
pis = tis // 2
piss = tis % 2
if tis == 1:
piss = 1
print(piss,tiss,afters)
else:
piss = tis % 2
tiis = pis // 2
tiiss = pis % 2
if pis == 1:
tiiss = 1
print(tiiss,piss,tiss,afters)
else:
tiiss = pis % 2
wins = tiis // 2
winss = tiis % 2
if tiis == 1:
winss = 1
print(winss,tiiss,piss,tiss,afters)
else:
winss = tiis % 2
pos = wins // 2
poss = wins % 2
if wins == 1:
poss = 1
print(poss,winss,tiiss,piss,tiss,afters)
else:
poss = wins % 2
wies = pos // 2
wiess = pos % 2
if pos == 1:
wiess = 1
print(wiess,poss,winss,tiiss,tiss,piss,afters)
else:
wiess = pos % 2
print(wiess,poss,winss,tiiss,piss,tiss,afters)
我自己又重新来了一次 但是 100和98验证的时候 和正确答案不对 青出于蓝 发表于 2021-7-11 17:18
欢迎追问
你这个不能运行 痴漫 发表于 2021-7-11 18:37
你这个不能运行
怎么会... 痴漫 发表于 2021-7-11 18:37
你这个不能运行
可以运行 青出于蓝 发表于 2021-7-11 18:58
可以运行
大佬麻烦看下我新改的 python内置函数bin()
Help on built-in function bin in module builtins:
bin(number, /)
Return the binary representation of an integer.
>>> bin(2796202)
'0b1010101010101010101010'
input要传入字符串,所以前面那个人的不行
改成这样
c=int(input('N:'))
print(bin(c))
页:
[1]