- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试制作一个简单的掷骰子游戏,其中两名玩家在五轮内掷一次骰子,得分最高的人获胜。
我已经尝试在函数内和函数外将 score1 变量设置为 0,但这将导致分数每次都重置为 0。
#setting the scores as 0 before.
score1=0
score2=0
def round1():
print('Round 1, lets go')
input("Player 1 press ENTER to roll dice")
diceroll1()
input("Player 2 press ENTER to roll dice")
diceroll2()
round2()
#Round 1, calls upon dice roll functions and.
#dice roll function for player 1
def diceroll1():
import random
number = random.randint(1,6)
print("Your number is: " + str(number))
if number % 2 == 0:
number = number + 10
score1 = score1 + number
print("Your new score is: " + str(score1))
else:
number = number - 5
score1 = score1 + number
print("Your new score is: " + str(score1))
if score1 < 0:
score1 = 0
else:
score1=score1
#dice roll function for player 2
def diceroll2():
import random
number = random.randint(1,6)
print("Your number is: " + str(number))
score2
if number % 2 == 0:
number = number + 10
score2 = score2 + number
print("Your new score is: " + str(score2))
else:
number = number - 5
score2 = score2 + number
print("Your new score is: " + str(score2))
if score2 < 0:
score2 = 0
else:
score2=score2
我希望它简单地将骰子值添加到分数中,但出现此错误:
UnboundLocalError: local variable 'score1' referenced before assignment
最佳答案
您应该使用global
标识符。
关于代码的一些说明:
score2 = score2
。只是不要这样做。我修改了您的代码并在其中留下了一些有趣的功能,这对您将来和现在都有帮助。
from random import randint
#setting the scores as 0 before.
score1=0
score2=0
#dice roll function for player 1
def diceroll1():
global score1
import random
number = randint(1,6)
print(f"Your number is: {str(number)}")
number += - 5 if number % 2 else 10
score1 += number
if score1 < 0:
score1 = 0
print(f"Your new score is: {str(score1)}")
#dice roll function for player 2
def diceroll2():
global score2
import random
number = randint(1,6)
print(f"Your number is: {str(number)}")
number += - 5 if number % 2 else 10
score2 += number
if score2 < 0:
score2 = 0
print(f"Your new score is: {str(score2)}")
def game_loop():
for _ in range(int(input("Raound number: "))):
print('Round 1, lets go')
input("Player 1 press ENTER to roll dice")
diceroll1()
input("Player 2 press ENTER to roll dice")
diceroll2()
print()
if __name__ == "__main__":
game_loop()
接下来,尝试将这两个功能合二为一。
关于Python 错误 : UnboundLocalError: local variable 'score1' referenced before assignment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56683400/
我不确定这个语法是否是 Informix 特有的,但是我在创建触发器时遇到了麻烦,直到我找到了一些包含这些行之一或两者的语法: CREATE TRIGGER accuplacer_trig
我是 Android Studio 和 gradle 的新手。我有一个 Android 项目,部分在 Eclipse 中工作。原始代码在Android中使用derby数据库。旧代码通过 JDBC 直接
我对以下示例中的行为感到困惑。我在一个项目中偶然发现了这个问题,我花了几个小时将问题缩小到一个简单的例子。所以这是我的简单测试类: 'John']]; public function inf
我在 pgAdmin 4 中创建了一些表,但由于某种原因我一直收到标题错误,你能找出原因吗?我没有看到任何问题,并且我已经查看了与我的代码示例相似的其他代码示例,这些示例编译得很好。它在 IDEone
我正在创建一个表,但出现此错误: number of referencing and referenced columns for foreign key disagree. 不知道怎么解决。我认为声
假设我有一个名为“C”的库 (.NETStandard 2.0),它定义了一个名为“CRecord”(记录)的类型。 假设我从名为“B”的 .NET 4.7.2 库中使用这个库。有一个使用“CReco
我已经尝试解决这个错误几分钟了,但我不知道表定义中缺少什么。 表格的代码如下: 表Autocare: CREATE TABLE [dbo].[Autocare] ( [IDAutocar]
DROP DATABASE IF EXISTS ProviderPatients; CREATE DATABASE ProviderPatients; USE ProviderPatients; CR
假设我有这个表stuff_property: | stuff_id (fk) | property_id | | ------------- | ----------- | 现在我想要进行查询,该查询
我是 MySQL 的新手。我完全有能力进行查询和创建表,但之前从未尝试过触发器。 CREATE TRIGGER TrigMora AFTER INSERT ON cliente REFEREN
我可以创建一个具有四个这样的属性的对象 $pocketKnife = New-Object PSObject -property @{ Color = 'Black' Weight =
我有一个名为 App 的对象,它包含主干应用程序的所有相关部分。 问题:当我从应用程序中的其他对象调用应用程序中的对象时,它们是未定义的。 我认为发生这种情况是因为在定义其自身的 App 对象之前,它
我有一个ArrayClass并且mergeSortArray扩展了它。并且 mergeSortArray 包含一个 mergeSort() 方法。但是,由于我使用 super 从父类(super cl
public class foo{ private String label; foo(String whereto){ label = whereto; } publi
我正在尝试以编程方式将库添加到引用的库中。这是我的代码: String filename = "myfile.jar"; InputStream is; try { is = new Buffe
我使用@Reference来获取我需要的所有信息: 吗菲亚: Query query = INSTANCE.createQuery(User.class); return query.asLi
我在 eclipse 中有一个 Java 项目,我想在其中添加 3 个 jar 文件到构建路径: 但是,当我选择它们并将它们添加到构建路径(右键单击/构建路径/添加到构建路径)时,它们将与成为类的“j
希望获得 Java 遵循的一些幕后内存引用和规则。 这是一段代码。基本上,此类用于实例化一些其他对象 (MyOtherObject),然后将此对象的 doClose() 方法的引用发送到 Vector
我打开了this关于转发引用的帖子,这是一个(希望如此)MCVE 代码: #include #include using namespace std; struct MultiMemoizator
MySQL 表: categoryID categoryName categoryParent 每个类别都有一个父类别,尽管它可以是 NULL,我将其视为根类别。 我想从表中获取所有类别,将其存储
我是一名优秀的程序员,十分优秀!