Python:MyVariable=MyVariable+;1不堆叠?
我目前正在做一个石头剪纸程序,它有一个随机选择的制作者,并重复自己。我试图实现一个游戏计数器和赢计数器。但每次它都会将自身重置为零。我尝试过很多方法,也尝试过搜索,但还没有找到解决方法Python:MyVariable=MyVariable+;1不堆叠?,python,Python,我目前正在做一个石头剪纸程序,它有一个随机选择的制作者,并重复自己。我试图实现一个游戏计数器和赢计数器。但每次它都会将自身重置为零。我尝试过很多方法,也尝试过搜索,但还没有找到解决方法 def everthing(): import time from random import randint original = input("Enter 'Rock', 'Paper' or 'Scisors: ") word = original.lower()
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 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 == 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 Win\n")
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\u win\u count=1
而不是+=1
你的变量在函数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\u win\u count=1
而不是+=1
你的变量在函数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\u win\u count=1
而不是+=1
你的变量在函数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\u win\u count=1
而不是+=1
问题是您在函数中定义了这些计数器。一旦函数退出,所有这些值都将被丢弃。要解决此问题,请让该函数返回更新的值,然后在调用时保存它们
def everything():
...
return player, pc, games
player_wins, pc_wins, games = everything()
此外,导入应该在全局范围内(在程序顶部),而不是在函数中。问题在于您在函数中定义这些计数器。一旦函数退出,所有这些值都将被丢弃。要解决此问题,请让该函数返回更新的值,然后在调用时保存它们
def everything():
...
return player, pc, games
player_wins, pc_wins, games = everything()
此外,导入应该在全局范围内(在程序顶部),而不是在函数中。问题在于您在函数中定义这些计数器。一旦函数退出,所有这些值都将被丢弃。要解决此问题,请让该函数返回更新的值,然后在调用时保存它们
def everything():
...
return player, pc, games
player_wins, pc_wins, games = everything()
此外,导入应该在全局范围内(在程序顶部),而不是在函数中。问题在于您在函数中定义这些计数器。一旦函数退出,所有这些值都将被丢弃。要解决此问题,请让该函数返回更新的值,然后在调用时保存它们
def everything():
...
return player, pc, games
player_wins, pc_wins, games = everything()
此外,导入应该在全局范围内(在程序的顶部),而不是在函数内。Thom正确地说,您的问题可以用全局变量来解决,但还有一个更大的潜在问题:通过反复调用函数本身来进行函数循环,最终将导致“超出最大递归深度”用户播放999轮后出错。如果希望函数循环,只需使用
for
或while
循环即可。这将顺便解决您的值重置问题。此外,如果您希望在正确的时间显示“that is not a valid entry”(这不是有效条目)消息,您可能应该使用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 tie\n")
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 Win\n")
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")
Thom说,你的问题可以用全局变量来解决,这是正确的,但还有一个更大的潜在问题:通过反复调用函数本身来进行循环,最终会在用户玩999轮后导致“超过最大递归深度”错误。如果希望函数循环,只需使用
for
或while
循环即可。这将顺便解决您的值重置问题。此外,如果您希望在正确的时间显示“that is not a valid entry”(这不是有效条目)消息,您可能应该使用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 tie\n")
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 Win\n")
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")
Thom说,你的问题可以用全局变量来解决,这是正确的,但还有一个更大的潜在问题:通过反复调用函数本身来进行循环,最终会在用户玩999轮后导致“超过最大递归深度”错误。如果希望函数循环,只需使用
for
或while
循环即可。这将顺便解决您的值重置问题。此外,如果您希望在正确的时间显示“that is not a valid entry”(这不是有效条目)消息,您可能应该使用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 tie\n")
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 Win\n")
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")
Thom说,您的问题可以用全局变量来解决,这是正确的,但还有一个更大的潜在问题:通过反复调用函数本身来实现函数循环,最终会在u