#RoShamBo
import random
count=0
while count<2 and count> -2:
compnum=random.randint(0,2)
usernum=int(input("Scissor(0), Rock(1), Paper(2)"))
if compnum==0:
if usernum==0:
print("Draw")
elif usernum==1:
print("Win")
count=count+1
elif usernum==2:
print("Lose")
count=count-1
elif compnum==1:
if usernum==0:
print("Lose")
count=count-1
elif usernum==1:
print("Draw")
elif usernum==2:
print("Win")
count=count+1
elif compnum==2:
if usernum==0:
print("Win")
count=count+1
elif usernum==1:
print("Lose")
count=count-1
elif usernum==2:
print("Draw")
if count>2:
print("You won more than 2 times")
else:
print("The computer won more than 2 times")
輸出結果搞砸了 - 一方面,它不會讓用戶獲勝。此外,它不正確計算數字。這是一本介紹Python的實驗班,但我相信教授寫錯了代碼。下面是一個示例破輸出:搖滾,紙,剪刀不讓用戶贏得
============== RESTART: C:/Users/FieryAssElsa/Desktop/Broken.py ==============
Scissor(0), Rock(1), Paper(2)2
Draw
Scissor(0), Rock(1), Paper(2)2
Win
Scissor(0), Rock(1), Paper(2)2
Draw
Scissor(0), Rock(1), Paper(2)2
Lose
Scissor(0), Rock(1), Paper(2)2
Win
Scissor(0), Rock(1), Paper(2)2
Win
The computer won more than 2 times
試試能否請你解釋一下? –
您將*總是*看到'電腦贏得了2次以上',因爲循環一直持續到'count'爲2或-2,然後達到條件。既然'count'是2或-2並且2不大於2或-2它會去else塊 – Li357