Bit_128 = 0
Bit_64 = 0
Bit_32 = 0
Bit_64 = 0
Bit_32 = 0
Bit_16 = 0
Bit_8 = 0
Bit_4 = 0
Bit_2 = 0
Bit_1 = 0
Number = int(input("Enter number to be converted: "))
for power in range(7,0,-1):
if (2**power) <= Number:
place_value = 2**power
if place_value == 128:
Bit_128 = 1
elif place_value == 64:
Bit_64 = 1
elif place_value == 32:
Bit_32 = 1
elif place_value == 16:
Bit_16 = 1
elif place_value == 8:
Bit_8 = 1
elif place_value == 4:
Bit_4 = 1
elif place_value == 2:
Bit_2 = 1
elif place_value ==1:
Bit_1 = 1
Number = Number - (place_value)
if Number == 0:
print ("Binary form of"),Number,("is"),Bit_128,Bit_64,Bit_32,Bit_16,Bit_8,Bit_4,Bit_2,Bit_1
我希望这个循环转移到下一个“电源”。当第一个if条件失败时的值,但是当我在解释器中运行它时,程序会继续运行,尽管第一个条件不是真的。如果第一个条件证明是真的,我只希望它继续下一个条件。 我怎样才能做到这一点?这是我的第一个&#34;大&#34; python中的程序,我很难搞清楚这一点。任何提示将不胜感激。顺便说一句,该程序旨在将任何数字从1-255转换为二进制形式。
答案 0 :(得分:0)
如果您希望循环转到下一个值,您只需使用continue关键字:
...
for power in range(7,0,-1):
if (2**power) <= Number:
place_value = 2**power
continue
...
答案 1 :(得分:0)
只需使用&#34; break&#34;声明,在条件满足时打破当前循环。