简体   繁体   English

在石头剪刀布python游戏中保持带循环得分的问题

[英]Issue with keeping the score with loops in rock paper scissors python game

I want to code a rock paper scissors game with a scoring system that allows the user to wish to play again and the score adds on and not restarting to 0. 我想用一个计分系统编写一个石头剪刀布游戏,该计分系统允许用户希望再次玩游戏,并且分数增加并且不会重新开始为0。

My code is here: https://pastebin.com/eRqEuwtY (It's also attached on this message) 我的代码在这里: https : //pastebin.com/eRqEuwtY (此消息中也随附)

Your help is appreciated. 感谢您的帮助。

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()

Your problem is that you print the score + 1 if you win. 您的问题是,如果获胜,则打印分数+ 1。 This does not save the new score to the variable! 这不会将新分数保存到变量中!

Example: 例:

print("You won")
score += 1
print("Score: You =", score1, "Computer =", score2)

Another problem in your code is that you call the main function again every time the user wants to play again. 代码中的另一个问题是,每次用户想要再次播放时,您都要再次调用main函数。 This is endless recursion and will result in an error if you hit the recursion limit. 这是无限的递归,如果您达到递归限制,将导致错误。 It's better to do it like this: 最好这样做:

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()

As Jasper pointed out, you need to save the new score to the respective variable. 正如Jasper指出的那样,您需要将新分数保存到相应的变量中。 Here is a rework of your code, including some improvements: 这是对代码的重做,包括一些改进:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM