假设我得到1到127之间的随机数.我将数字更改为二进制,并0b使用以下代码删除它:
key_one= int(raw_input("Enter key (0 <= key <= 127): "))
if key_one in range(128):
bin_key_one=bin(key_one)[2:]
print bin_key_one
else:
print "You have to enter key (0 <= key <= 127)"
Run Code Online (Sandbox Code Playgroud)
现在我想通过在必要时用零填充开头来使它长7个字符.我想我需要使用for循环,但是有人可以告诉我该怎么做吗?
如何在python中随机随机播放单词的字母?
例如,单词"cat"可能会更改为"act","tac"或"tca".
我想这样做而不使用内置函数
在python中,fachlowing是一个tic tak toe游戏代码,有些人可以告诉我如何使用重置选项以GUI形式制作它并显示最终获胜者.喜欢X胜利还是O胜利?
board ="1 | 2 | 3 \n ----------- \n 4 | 5 | 6 \n ----------- \n 7 | 8 | 9"
.棋盘格= [1,2,3,4,5,6,7,8,9,1,4,7,2,5,8,3,6,9,1,5,9,3,5,7 ]
空格=范围(1,10)
def moveHandler(棋盘,空格,棋盘,球员,n):
if player==1:
check="X"
else:
check="O"
while spaces.count(n)==0:
print "\nInvalid Space"
n=playerinput(player)
spaces=spaces.remove(n)
board=board.replace(str(n),check)
for c in range(len(checkboard)):
if checkboard[c]==n:
checkboard[c]=check
status = checkwinner(checkboard,check)
return board,status
Run Code Online (Sandbox Code Playgroud)
def checkwinner(checkboard,check):a,b,c = 0,1,2
while a<=21:
combo = [checkboard[a],checkboard[b],checkboard[c]]
if combo.count(check) == 3:
status =1
break
else:
status =0
a+=3
b+=3
c+=3
return status …Run Code Online (Sandbox Code Playgroud)