我的代碼中的一切都是正確的,但是在執行第二個回圈 2 次或多次運行后,系統選擇的值是別的東西,之后的值(加/減)是別的東西..即使在這兩種情況下,使用的變數都是相同的……
import random
player_1='Mr.BOT'
player_2=input('Enter your name : ')
print(" RULES \n(1)Each player will be given 9 marbles\n(2)One-by-One each player will hold some of their\n marbles(it's upto them).\n(3)Other player will have to guess that if the number\n of marbles that was picked by them is even or odd.\n(4)If the guess made by the other player is correct,\n the player holding the marbles will have to give him\n the marbles that he was holding,but if the other player\n will make a wrong guess that player will now have to\n give the number of marbles that the player was holding\n to that player himself..")
while True:
while True:
P_1=9
P_2=9
a=random.randint(1, P_1)
if a%2==0:
c='even'
else:
c='odd'
d=input('Make your choice (even/odd) : ')
if d==c:
print('Your guess was correct....!!')
P_1-=a
P_2 =a
else:
print('You made a wrong guess....!!')
P_1 =a
P_2-=a
print('Current status.......')
print(player_1,' ',player_2)
print(' ',P_1,' ',P_2)
if P_1<=0 or P_2<=0:
if P_1<=0:
print(player_1,'LOSE.....??')
break
else:
print(player_2,'LOSE.....??')
break
a1=int(input('How many marbles you want to take in your hands : '))
while a1>P_2 or a1<=0:
print('please enter a valid number....!!')
a1=int(input('How many marbles you want to take in your hands : '))
if a1>P_2 or a1<=0:
continue
break
b1=a1%2
if b1==0:
c1='even'
else:
c1='odd'
D=['even','odd']
d1=random.choice(D)
if d1==c1:
print('Mr.Bot made a correct guess....!!')
P_2-=a1
P_1 =a1
else:
print('Mr.Bot made a wrong guess....!!')
P_2 =a1
P_1-=a1
print('Current status.......')
print(player_1,' ',player_2)
print(' ',P_1,' ',P_2)
if P_1<=0 or P_2<=0:
if P_1<=0:
print(player_1,' LOSE.....??')
break
else:
print(player_2,' LOSE.....??')
break
again = input('Wanna play again(y/n) : ')
if again=='n':
print('Thanks for playing with me ????')
break
這里可能有什么問題?
uj5u.com熱心網友回復:
問題是兩個玩家的分數在每次運行內while回圈的頂部被重置為 9 個彈珠。因此,在 Bot 先生做出猜測并調整分數后,在要求玩家進行猜測之前,分數會被重置為 9。
while當玩家說他們不想再玩時,外回圈被打破,while當當前正在進行的游戲結束時,內回圈被打破。
移動重置的行,P_1使P_2它們僅在外while回圈內。換句話說,替換
while True:
while True:
P_1=9
P_2=9
# rest of code omitted...
和
while True:
P_1=9
P_2=9
while True:
# rest of code omitted...
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/429191.html
標籤:Python python-3.x python-3.10
上一篇:CSV檔案聚合后洗掉換行符
