- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用 python 3.4.3 制作一个简单的二十一点游戏。到目前为止,我已经介绍了一些选项,例如 split 或 ace 可以是 1 或 11。我得到的下一张牌让我破产然后它会说我破产但是如果我不破产那么下一个函数(housePlay())不起作用,你知道为什么吗?这是错误:
Traceback (most recent call last):
File "F:\Computing\Black Jack.py", line 112, in <module>
housePlay()
File "F:\Computing\Black Jack.py", line 78, in housePlay
print("The house have a",c,"and a",d,"giving them a total of",houseCount)
UnboundLocalError: local variable 'houseCount' referenced before assignment
p.s 我对编码很陌生,所以请使用简单的术语,以便我能理解你
import random
playerCount=0
houseCount=0
cards={"Ace of Hearts":1,
"Two of Hearts":2,
"Three of Hearts":3,
"Four of Hearts":4,
"Five of Heats":5,
"Six of Hearts":6,
"Seven of Hearts":7,
"Eight of Hearts":8,
"Nine of Hearts":9,
"Ten of Hearts":10,
"Jack of Hearts":10,
"Queen of Hearts":10,
"King of Hearts":10,
"Ace of Diamonds":1,
"Two of Diamonds":2,
"Three of Diamonds":3,
"Four of Diamonds":4,
"Five of Diamonds":5,
"Six of Diamonds":6,
"Seven of Diamonds":7,
"Eight of Diamonds":8,
"Nine of Diamonds":9,
"Ten of Diamonds":10,
"Jack of Diamonds":10,
"Queen of Diamonds":10,
"King of Diamonds":10,
"Ace of Spades":1,
"Two of Spades":2,
"Three of Spades":3,
"Four of Spades":4,
"Five of Spades":5,
"Six of Spades":6,
"Seven of Spades":7,
"Eight of Spades":8,
"Nine of Spades":9,
"Ten of Spades":10,
"Jack of Spades":10,
"Queen of Spades":10,
"King of Spades":10,
"Ace of Clubs":1,
"Two of Clubs":2,
"Three of Clubs":3,
"Four of Clubs":4,
"Five of Clubs":5,
"Six of Clubs":6,
"Seven of Clubs":7,
"Eight of Clubs":8,
"Nine of Clubs":9,
"Ten of Clubs":10,
"Jack of Clubs":10,
"Queen of Clubs":10,
"King of Clubs":10}
temp = []
for i in cards:
temp.append(i)
a=random.choice(temp)
b=random.choice(temp)
c=random.choice(temp)
d=random.choice(temp)
f=random.choice(temp)
while a == b:
b=random.choice(temp)
while c == b or c== a:
c=random.choice(temp)
while d == b or d== a or d==c:
d=random.choice(temp)
while f == a or f == b or f == c or f == d:
e=random.choice(temp)
playerCount+=cards[a]
playerCount+=cards[b]
houseCount+=cards[c]
def housePlay():
print("The house have a",c,"and a",d,"giving them a total of",houseCount)
if houseCount<17:
print("The house hits and gets a",f)
houseCount+=cards[f]
if houseCount>21:
print("The house go bust, you win")
else:
if playerCount>houseCount:
print("Unlucky, the house total is larger than yours so you lose")
elif playerCount==houseCount:
print("You have then same total as the house so you get your money back")
else:
print("Your total is larger than the house's so you win")
else:
print("The house stay with a total of",houseCount)
if playerCount>houseCount:
print("Unlucky, the house total is larger than yours so you lose")
elif playerCount==houseCount:
print("You have then same total as the house so you get your money back")
else:
print("Your total is larger than the house's so you win")
print("Your cards:",a," and ",b,"Which add to ",playerCount)
print("Dealers card:",c,"Which adds to ",houseCount)
play=input("Would you like to Hit or Stand?")
if play=="hit":
e=random.choice(temp)
while e == a or e== b or e==c or e==d or e == f:
e=random.choice(temp)
playerCount+=cards[e]
if playerCount>21:
print("You were dealt a",e)
print("Unlucky you have gone bust")
else:
housePlay()
elif play=="stand":
houseCount+=cards[d]
if houseCount<17:
housePlay()
else:
if playerCount>houseCount:
print("Unlucky, the house total is larger than yours so you lose")
elif playerCount==houseCount:
print("You have then same total as the house so you get your money back")
else:
print("Your total is larger than the house's so you win")
最佳答案
def housePlay(houseCount):
在函数定义中将 houseCount 作为参数传递
housePlay(houseCount)
像这样调用 housePlay 函数。
关于python - 它说我定义的变量是 "referenced before assignment",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38635843/
我不确定这个语法是否是 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,我将其视为根类别。 我想从表中获取所有类别,将其存储
我是一名优秀的程序员,十分优秀!