我想检查我的程序中的多个条件,但由于某些原因它无法正常工作。这是我的代码:
def removingStrawsComputer():
removedNumber=random.randint(1,3)
global strawsNumber
if strawsNumber==3:
removedNumber=random.randint(1,2)
if strawsNumber==2:
removedNumber==1
strawsNumber-=removedNumber
return strawsNumber
执行我的代码时,即使当strawsNumber = 3时,random.randint有时会选择3 ..任何想法我做错了什么?任何帮助将非常感激.. :)
编辑: 这是我的整个代码,它是一个NIM游戏。有多个if if,我试图避免如果剩下的吸管是3,计算机选择3并立即丢失..
player1=raw_input("Iveskite savo varda: ")
player2="Kompiuteris"
howMany=0
gameover=False
strawsNumber=random.randint(10,20)
def removingStrawsComputer():
removedNumber=random.randint(1,3)
global strawsNumber
if strawsNumber==3:
removedNumber=random.randint(1,2)
if strawsNumber==2:
removedNumber==1
strawsNumber-=removedNumber
return strawsNumber
def removingStrawsHuman():
global strawsNumber
strawsNumber-=howMany
return strawsNumber
def humanLegalMove():
global howMany
legalMove=False
while not legalMove:
print("Ejima atlieka ",player1)
howMany=int(input("Kiek objektu nori paimti?(nuo 1 iki 3) "))
if howMany>3 or howMany<1:
print("Iveskite skaiciu nuo 1 iki 3")
else:
legalMove=True
while howMany>strawsNumber:
print("Ivestas skaicius didesnis nei liko objektu")
howMany=int(input("Kiek objektu nori paimti?(nuo 1 iki 3) "))
return howMany
def checkWinner(player):
if strawsNumber==0:
print(player," laimejo.")
global gameover
gameover=True
return gameover
def resetGameover():
global gameover
gameover=False
return gameover
def game():
global strawsNumber
strawsNumber=random.randint(10,20)
print("Objektu skaicius ",strawsNumber)
while gameover==False:
print("Ejima atlieka ",player2,". Objektu liko: ",removingStrawsComputer())
checkWinner(player1)
if gameover==True:
break
humanLegalMove()
print("Objektu liko: ",removingStrawsHuman())
checkWinner(player2)
def playAgain():
answer=raw_input("Ar norite zaisti dar karta?(y/n)")
resetGameover()
while answer=="y":
game()
answer=raw_input("Ar norite zaisti dar karta?(y/n)")
else:
print("Aciu uz zaidima!")
game()
playAgain()
答案 0 :(得分:0)
我认为这就是你要找的东西:
def removingStrawsComputer():
removedNumber=random.randint(1,3)
global strawsNumber
if strawsNumber==1 and random.randint(0,1)==1:
return 0
strawsNumber-=removedNumber
if strawsNumber<1:
strawsNumber=1
return strawsNumber