Python: MyVariable = MyVariable + 1 not stacking?



我目前正在开发一个石头剪刀程序,它有一个随机选择生成器,并重复它自己。我正在尝试实现Game计数器和Win计数器。但每次它都会将自己重置为零。我尝试了很多事情,也尝试过搜索,但还没有想出如何解决这个问题。

def everthing():
    import time
    from random import randint
    original = input("Enter 'Rock', 'Paper' or 'Scisors: ")
    word = original.lower()
    Player_win_count = 0
    PC_win_count = 0
    Game_count = 0
    if word == "rock":
        no = randint(1,3)
        if no == 1:
            print ("The Computer chose Rock. It's a tien")
            Game_count += 1
            print ("The games played are: " + str(Game_count) + "n      The scores are:n                 You: " + str(Player_win_count) + "n            Computer: " + str(PC_win_count))
        if no == 2:
            print ("The Computer chose Paper. You Lose!n")
            Game_count += 1
            PC_win_count += 1
            print ("The games played are: " + str(Game_count) + "n      The scores are:n                 You: " + str(Player_win_count) + "n            Computer: " + str(PC_win_count))
        if no == 3:
            print ("The Computer chose Scisors. You win!n")
            Game_count += 1
            Player_win_count = 1
            print ("The games played are: " + str(Game_count) + "n      The scores are:n                 You: " + str(Player_win_count) + "n            Computer: " + str(PC_win_count))
    if word == "paper":
        no = randint(1,3)
        if no == 1:
            print ("The Computer chose Rock. You Winn")
            Player_win_count += 1
            Game_count += 1
            print ("The games played are: " + str(Game_count) + "n      The scores are:n                 You: " + str(Player_win_count) + "n            Computer: " + str(PC_win_count))
        if no == 2:
            print ("The Computer chose Paper. It's a tie!n")
            Game_count += 1
            print ("The games played are: " + str(Game_count) + "n      The scores are:n                 You: " + str(Player_win_count) + "n            Computer: " + str(PC_win_count))
        if no == 3:
             print ("The Computer chose Scisors. You lose!n")
             PC_win_count += 1
             Game_count += 1
             print ("The games played are: " + str(Game_count) + "n      The scores are:n                 You: " + str(Player_win_count) + "n            Computer: " + str(PC_win_count))
    if word == "scisors":
        no = randint(1,3)
        if no == 1:
            print ("nThe Computer chose Rock. You Lose!n")
            PC_win_count += 1
            Game_count += 1
            print ("The games played are: " + str(Game_count) + "n      The scores are:n                 You: " + str(Player_win_count) + "n            Computer: " + str(PC_win_count))
        if no == 2:
            print ("nThe Computer chose Paper. You Win!n")
            PC_win_count += 1
            Game_count += 1
            print ("The games played are: " + str(Game_count) + "n      The scores are:n                 You: " + str(Player_win_count) + "n            Computer: " + str(PC_win_count))
        if no == 3:
            print ("nThe Computer chose Scisors. It's a tie!n")
            Game_count += 1
            print ("The games played are: " + str(Game_count) + "n      The scores are:n                 You: " + str(Player_win_count) + "n            Computer: " + str(PC_win_count))
        everything()

    else:
        print ("That is not a valid entry. Please try again")
        everything()
    everything()

您的变量在函数everything()的范围内。这意味着每次你再次调用函数时,它们都会被初始化。您应该在函数的外部定义它们,这样它们只有在首次加载程序时才会初始化。

(您还应该将导入移到文件的顶部,否则每次everything()调用都会再次导入它们)

示例:

def a():
    myvar_a = 10
    myvar_a += 1
    print(myvar_a)  # will print 11, every time
myvar_b = 10
def b():
    global myvar_b  # otherwise we can only read myvar_b and
                    # ... not reassign it to myvar_b = myvar_b + 1
    myvar_b += 1
    print(myvar_b)  # will print 11, 12, 13....

顺便说一下,还有一行是Player_win_count = 1而不是+= 1

Thom说你的问题可以用全局变量来解决是正确的,但还有一个更大的潜在问题:通过反复调用自己来进行函数循环,最终会在用户玩999轮后导致"超过最大递归深度"错误。如果希望函数循环,只需使用forwhile循环即可。这将顺便解决您的值重置问题。此外,如果您希望您的"那不是一个有效的条目"消息在正确的时间出现,您可能应该使用elif而不是else

import time
from random import randint
Player_win_count = 0
PC_win_count = 0
Game_count = 0
while True:
    original = input("Enter 'Rock', 'Paper' or 'Scisors: ")
    word = original.lower()
    no = randint(1,3)
    if word == "rock":
        if no == 1:
            print ("The Computer chose Rock. It's a tien")
            Game_count += 1
        if no == 2:
            print ("The Computer chose Paper. You Lose!n")
            Game_count += 1
            PC_win_count += 1
        if no == 3:
            print ("The Computer chose Scisors. You win!n")
            Game_count += 1
            Player_win_count += 1
        print ("The games played are: " + str(Game_count) + "n      The scores are:n                 You: " + str(Player_win_count) + "n            Computer: " + str(PC_win_count))
    elif word == "paper":
        if no == 1:
            print ("The Computer chose Rock. You Winn")
            Player_win_count += 1
            Game_count += 1
        if no == 2:
            print ("The Computer chose Paper. It's a tie!n")
            Game_count += 1
        if no == 3:
            print ("The Computer chose Scisors. You lose!n")
            PC_win_count += 1
            Game_count += 1
        print ("The games played are: " + str(Game_count) + "n      The scores are:n                 You: " + str(Player_win_count) + "n            Computer: " + str(PC_win_count))
    elif word == "scisors":
        if no == 1:
            print ("nThe Computer chose Rock. You Lose!n")
            PC_win_count += 1
            Game_count += 1
        if no == 2:
            print ("nThe Computer chose Paper. You Win!n")
            PC_win_count += 1
            Game_count += 1
        if no == 3:
            print ("nThe Computer chose Scisors. It's a tie!n")
            Game_count += 1
        print ("The games played are: " + str(Game_count) + "n      The scores are:n                 You: " + str(Player_win_count) + "n            Computer: " + str(PC_win_count))

    else:
        print ("That is not a valid entry. Please try again")

问题是在函数中定义这些计数器。一旦函数退出,所有这些值都将被丢弃。为了解决这个问题,让该函数返回更新后的值,然后在调用它时保存它们

def everything():
    ...
    return player, pc, games
player_wins, pc_wins, games = everything()

此外,导入应该在全局范围内(在程序的顶部),而不是在函数内。

最新更新