[英]Issue with keeping the score with loops in rock paper scissors python game
我想用一個計分系統編寫一個石頭剪刀布游戲,該計分系統允許用戶希望再次玩游戲,並且分數增加並且不會重新開始為0。
我的代碼在這里: https : //pastebin.com/eRqEuwtY (此消息中也隨附)
感謝您的幫助。
import random
score1 = int(0)
score2 = int(0)
def main():
while True:
player = input('What do you choose to play (R, P, S)? ')
computer = random.choice(["R", "P", "S"])
print("You chose", (player), "and the computer chose", (computer))
if computer == player:
print("It's a tie")
print("Score: You =", score1, "Computer =", score2)
if computer == "R" and player == "S":
print("Computer wins")
print("Score: You =", score1, "Computer =", score2 + 1)
if computer == "P" and player == "R":
print("Computer wins")
print("Score: You =", score1, "Computer =", score2 + 1)
if computer == "S" and player == "P":
print("Computer wins")
print("Score: You =", score1, "Computer =", score2 + 1)
if computer == "S" and player == "R":
print("You won")
print("Score: You =", score1 + 1, "Computer =", score2)
if computer == "R" and player == "P":
print("You won")
print("Score: You =", score1 + 1, "Computer =", score2)
if computer == "P" and player == "S":
print("You won")
print("Score: You =", score1 + 1, "Computer =", score2)
play_again = input("Would you like to play again? Y/N ")
if play_again == "Y":
main()
else:
print("You scored", score1, "and the computer scored", score2)
exit()
main()
您的問題是,如果獲勝,則打印分數+ 1。 這不會將新分數保存到變量中!
例:
print("You won")
score += 1
print("Score: You =", score1, "Computer =", score2)
代碼中的另一個問題是,每次用戶想要再次播放時,您都要再次調用main函數。 這是無限的遞歸,如果您達到遞歸限制,將導致錯誤。 最好這樣做:
def main():
play_again = "Y"
while play_again == "Y":
#code...
play_again = input("Would you like to play again? Y/N ")
print("You scored", score1, "and the computer scored", score2)
main()
正如Jasper指出的那樣,您需要將新分數保存到相應的變量中。 這是對代碼的重做,包括一些改進:
import random
def main():
SYMBOLS = ["R", "P", "S"]
INPUTS_YES = ["Y", "YES", "I LOVE THIS GAME SO MUCH"]
INPUTS_NO = ["N", "NO", "PLZ STOP THIS"]
INPUTS_ALL = INPUTS_YES + INPUTS_NO
keep_playing = True
score_player = 0
score_computer = 0
while keep_playing:
symbol_player = input("What do you choose to play? (R, P, S)").upper()
while not symbol_player in SYMBOLS:
print("invalid input")
symbol_player = input("What do you choose to play? (R, P, S)").upper()
symbol_computer = random.choice(SYMBOLS)
print("You chose ", symbol_player, " and the computer chose ", symbol_computer)
difference = (SYMBOLS.index(symbol_player) - SYMBOLS.index(symbol_computer)) % 3
if difference == 0:
print("It's a tie")
if difference == 1:
score_player += 1
print("You won")
if difference == 2:
score_computer += 1
print("Computer won")
print("Score: You = ", score_player, ", Computer = ", score_computer)
play_again = input("Would you like to play again? Y/N").upper()
while not play_again in INPUTS_ALL:
print("invalid input")
play_again = input("Would you like to play again? Y/N").upper()
if play_again in INPUTS_NO:
keep_playing = False
print("You scored", score_player, "and the computer scored", score_computer)
if __name__ == "__main__":
main()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.