Python简单的剪刀石头布游戏
Python simple rock, paper, scissors game
嘿,所以我是一名新手程序员,为了上学,我必须在 python 中制作一个简单的剪刀石头布游戏(3 个中最好的)。我的问题是分数系统,我收到以下两个错误:
Traceback (most recent call last):
File "c:\Users\Fabian\OneDrive\School\Python\Project stuff\RockPaperScissors\game.py", line 66, in <module>
game1()
File "c:\Users\Fabian\OneDrive\School\Python\Project stuff\RockPaperScissors\game.py", line 26, in game1
score_com = score_com + 1
UnboundLocalError: local variable 'score_com' referenced before assignment
这是我目前的代码:
import random
#variables
rock = 1
paper = 2
scissors = 3
#empty score
score = 0
score_com = 0
#game info
print("Rules and how to play: ")
print("The game is best out of 3")
print("If you wanna choose rock type 1, if you wanna choose paper type 2 and if you wanna use scissors type 3")
print("Dont worry if you forget what number represents what choice, there will be a reminder before you input a number")
#how the game works
def game1():
choice = input("Choose: rock, paper or scissors (1, 2 or 3)")
if choice == "1":
choice_com = random.randint(1, 3)
print(choice_com)
if choice_com == 1:
print("Tie!")
if choice_com == 2:
print("You lost!")
score_com = score_com + 1
if choice_com == 3:
print("You win!")
score = score + 1
elif choice == "2":
choice_com = random.randint(1, 3)
print(choice_com)
if choice_com == 1:
print("You win!")
if choice_com == 2:
print("Tie!")
score_com = score_com + 1
if choice_com == 3:
print("You lost!")
score = score + 1
elif choice == "3":
choice_com = random.randint(1, 3)
print(choice_com)
if choice_com == 1:
print("You lost!")
score_com = score_com + 1
if choice_com == 2:
print("You won!")
score = score + 1
if choice_com == 3:
print("Tie!")
#the order of how things are supposed to be displayed and a simple way of seeing the score count
start = input("Start? y/n")
if start == "y":
game1()
print("Score player 1: ")
print(score)
print("Score player 2: ")
print(score_com)
game1()
print("Score player 1: ")
print(score)
print("Score player 2: ")
print(score_com)
if score == 2:
print("Player 1 wins!")
exit()
if score_com == 2:
print("Player 2 wins!")
exit()
game1()
print("Score player 1: ")
print(score)
print("Score player 2: ")
print(score_com)
if score == 3:
print("Player 1 wins!")
if score_com == 3:
print("Player 2 wins!")
if score == 2:
print("Player 1 wins!")
if score_com == 2:
print("Player 2 wins!")
这个问题可能是一个非常简单的解决方法,但正如我所说,我是编程新手,所以我找不到解决方法。提前感谢任何帮助我的人!
一个简单的修复方法是在函数的开头添加以下行,以便它使用全局变量
def game1():
global score
global score_com
...
或者,您可以game1
同时获取分数变量和return更新后的分数。
您的函数 game1 正在使用 local
score_com 变量。
您应该在函数内部指明要使用全局变量。
def game1():
global score_com
或引用全局变量作为参数
def game1(score_com=score_com):
...
同变量score
默认情况下 Python,假定函数更改的任何变量都是函数的局部变量。如果你想改变像 score_com 这样的全局变量,你需要在函数中添加一个 global score_com
声明。在这种情况下,您还要更新 score
,因此也需要声明:
def game1():
global score, score_com
...
感谢所有回复的人!
我在变量前面添加了全局变量,如下所示,效果很好。
def game1():
global score, score_com
下次我遇到类似情况时,我会牢记所有答案:D
嘿,所以我是一名新手程序员,为了上学,我必须在 python 中制作一个简单的剪刀石头布游戏(3 个中最好的)。我的问题是分数系统,我收到以下两个错误:
Traceback (most recent call last):
File "c:\Users\Fabian\OneDrive\School\Python\Project stuff\RockPaperScissors\game.py", line 66, in <module>
game1()
File "c:\Users\Fabian\OneDrive\School\Python\Project stuff\RockPaperScissors\game.py", line 26, in game1
score_com = score_com + 1
UnboundLocalError: local variable 'score_com' referenced before assignment
这是我目前的代码:
import random
#variables
rock = 1
paper = 2
scissors = 3
#empty score
score = 0
score_com = 0
#game info
print("Rules and how to play: ")
print("The game is best out of 3")
print("If you wanna choose rock type 1, if you wanna choose paper type 2 and if you wanna use scissors type 3")
print("Dont worry if you forget what number represents what choice, there will be a reminder before you input a number")
#how the game works
def game1():
choice = input("Choose: rock, paper or scissors (1, 2 or 3)")
if choice == "1":
choice_com = random.randint(1, 3)
print(choice_com)
if choice_com == 1:
print("Tie!")
if choice_com == 2:
print("You lost!")
score_com = score_com + 1
if choice_com == 3:
print("You win!")
score = score + 1
elif choice == "2":
choice_com = random.randint(1, 3)
print(choice_com)
if choice_com == 1:
print("You win!")
if choice_com == 2:
print("Tie!")
score_com = score_com + 1
if choice_com == 3:
print("You lost!")
score = score + 1
elif choice == "3":
choice_com = random.randint(1, 3)
print(choice_com)
if choice_com == 1:
print("You lost!")
score_com = score_com + 1
if choice_com == 2:
print("You won!")
score = score + 1
if choice_com == 3:
print("Tie!")
#the order of how things are supposed to be displayed and a simple way of seeing the score count
start = input("Start? y/n")
if start == "y":
game1()
print("Score player 1: ")
print(score)
print("Score player 2: ")
print(score_com)
game1()
print("Score player 1: ")
print(score)
print("Score player 2: ")
print(score_com)
if score == 2:
print("Player 1 wins!")
exit()
if score_com == 2:
print("Player 2 wins!")
exit()
game1()
print("Score player 1: ")
print(score)
print("Score player 2: ")
print(score_com)
if score == 3:
print("Player 1 wins!")
if score_com == 3:
print("Player 2 wins!")
if score == 2:
print("Player 1 wins!")
if score_com == 2:
print("Player 2 wins!")
这个问题可能是一个非常简单的解决方法,但正如我所说,我是编程新手,所以我找不到解决方法。提前感谢任何帮助我的人!
一个简单的修复方法是在函数的开头添加以下行,以便它使用全局变量
def game1():
global score
global score_com
...
或者,您可以game1
同时获取分数变量和return更新后的分数。
您的函数 game1 正在使用 local
score_com 变量。
您应该在函数内部指明要使用全局变量。
def game1():
global score_com
或引用全局变量作为参数
def game1(score_com=score_com):
...
同变量score
默认情况下 Python,假定函数更改的任何变量都是函数的局部变量。如果你想改变像 score_com 这样的全局变量,你需要在函数中添加一个 global score_com
声明。在这种情况下,您还要更新 score
,因此也需要声明:
def game1():
global score, score_com
...
感谢所有回复的人!
我在变量前面添加了全局变量,如下所示,效果很好。
def game1():
global score, score_com
下次我遇到类似情况时,我会牢记所有答案:D