我正在尝试通过编码纸牌游戏21的版本来挑战自己,但是我在字符串和整数方面遇到了麻烦。我的柜台也没有用,那超出了我
我尝试将所有内容定义为整数
cardnum1 = " "
cardnum2 = " "
print("the dealer shuffles the cards and deals the first hand")
import random
list = ["2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"]
#1st card
card1 = random.choice(list)
print("your first card is ",card1)
#numbers
if card1 == ["2","3","4","5","6","7","8","9","10"]:
card1=int(cardnum1)
elif card1 == "Jack":
cardnum1=int(10)
elif card1 == "Queen":
cardnum1=int(10)
elif card1 == "King":
cardnum1=int(10)
elif card1 == "Ace":
cardnum1=input("do you want your Ace to be a 1 or a 11?")
card2 = random.choice(list)
print("your second card is ",card2)
#numbers 1 - 10
if card2 == ["2","3","4","5","6","7","8","9","10"]:
card1=int(cardnum1)
#special cards
elif card2 == "Jack":
cardnum2=int(10)
elif card2 == "Queen":
cardnum2=int(10)
elif card2 == "King":
cardnum2=int(10)
elif card2 == "Ace":
cardnum2=input("do you want your Ace to be a 1 or a 11?")
print ("your cards combined are ",cardnum1 + cardnum2)
我正尝试随机获得2张卡,并且让程序告诉我我距离21岁有多远,以及我是否破产。 cardnum1和cardnum2似乎没有正确添加。
我收到错误消息:
TypeError: can only concatenate str (not "int") to str
答案 0 :(得分:2)
提到的错误是由于这样的事实,在某些情况下,这些变量之一是字符串,而另一个是整数。
实际上您有几处错误的事情:
list
是python内置的,请勿将单词list
用作变量。if card1 == ["2","3","4","5","6","7","8","9","10"]
是错误的。要检查变量是否在列表中,请执行:if card1 in ["2","3","4","5","6","7","8","9","10"]
。请注意,in
而不是==
。card1=int(cardnum1)
是cardnum1
,则" "
没有意义。您可能已将它们切换为:cardnum1 = int(card1)
。cardnum1=int(10)
没错,但是10已经是一个整数。 int()
是多余的,完全不需要。cardnum2=input("do you want your Ace to be a 1 or a 11?")
我想在这里您希望用户键入1或11。如果您使用的是python3,input
总是返回一个字符串,因此必须将其转换为整数:{{1} }。如果您使用的是python2,就可以了。答案 1 :(得分:1)
您应该使用字典为卡片分配编号,并且可以使用from PyQt5 import QtCore, QtGui, QtWidgets, QtPrintSupport
from PyQt5.QtGui import QIcon, QPixmap
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.resize(1920, 1080)
self.Frame = QtWidgets.QWidget(MainWindow)
self.Bild1 = QtWidgets.QLabel(self.Frame)
self.Bild1.setGeometry(QtCore.QRect(20, 45, 600, 400))
pixmap1 = QPixmap('image1.JPG').scaled(600, 400)
self.Bild1.setPixmap(pixmap1)
self.Bild2 = QtWidgets.QLabel(self.Frame)
self.Bild2.setGeometry(QtCore.QRect(660, 45, 600, 400))
pixmap2 = QPixmap('image2.JPG').scaled(600, 400)
self.Bild2.setPixmap(pixmap2)
self.Bild3 = QtWidgets.QLabel(self.Frame)
self.Bild3.setGeometry(QtCore.QRect(1300, 45, 600, 400))
pixmap3 = QPixmap('image3.JPG').scaled(600, 400)
self.Bild3.setPixmap(pixmap3)
self.Bild4 = QtWidgets.QLabel(self.Frame)
self.Bild4.setGeometry(QtCore.QRect(20, 565, 600, 400))
pixmap4 = QPixmap('image4.JPG').scaled(600, 400)
self.Bild4.setPixmap(pixmap4)
self.Bild5 = QtWidgets.QLabel(self.Frame)
self.Bild5.setGeometry(QtCore.QRect(660, 565, 600, 400))
pixmap5 = QPixmap('image5.JPG').scaled(600, 400)
self.Bild5.setPixmap(pixmap5)
self.Bild6 = QtWidgets.QLabel(self.Frame)
self.Bild6.setGeometry(QtCore.QRect(1300, 565, 600, 400))
pixmap6 = QPixmap('image6.JPG').scaled(600, 400)
self.Bild6.setPixmap(pixmap6)
self.WeiterButton = QtWidgets.QPushButton(self.Frame)
self.WeiterButton.setGeometry(QtCore.QRect(1825, 1008, 75, 24))
MainWindow.setCentralWidget(self.Frame)
self.WeiterButton.clicked.connect(self.print_widget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Fotobox"))
self.WeiterButton.setText(_translate("MainWindow", "Print"))
def print_widget(self):
# Create printer
printer = QtPrintSupport.QPrinter()
# Create painter
painter = QtGui.QPainter()
# Start painter
painter.begin(printer)
# Grab a widget you want to print
screen = self.Bild1.grab()
# Draw grabbed pixmap
painter.drawPixmap(0, 0, screen)
# End painting
painter.end()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
选择2张卡片
random.choices
要轻松地将它们全部放入一个句子中,可以在Python 3.6+中使用import random
cards = {
1: "Ace",
2: "2",
3: "3",
4: "4",
5: "5",
6: "6",
7: "7",
8: "8",
9: "9",
10: "10",
11: "Jack",
12: "Queen",
13: "King"
}
print("the dealer shuffles the cards and deals the first hand")
card_num1, card_num2 = random.choices(list(cards), k=2)
card1, card2 = cards[card_num1], cards[card_num2]
print("your first card is ",card1)
print("your second card is ",card2)
或f字符串。
.format()
答案 2 :(得分:0)
正如我在评论中解释的那样,您错误地检查了列表包含,并且在if语句中设置了错误的变量。这是您的固定代码:
import random
cardnum1 = " "
cardnum2 = " "
print("the dealer shuffles the cards and deals the first hand")
list = ["2", "3", "4", "5", "6", "7", "8",
"9", "10", "Jack", "Queen", "King", "Ace"]
# 1st card
card1 = random.choice(list)
print("your first card is ", card1)
# numbers
if card1 in ["2", "3", "4", "5", "6", "7", "8", "9", "10"]:
cardnum1 = int(card1)
elif card1 == "Jack":
cardnum1 = int(10)
elif card1 == "Queen":
cardnum1 = int(10)
elif card1 == "King":
cardnum1 = int(10)
elif card1 == "Ace":
cardnum1 = input("do you want your Ace to be a 1 or a 11?")
card2 = random.choice(list)
print("your second card is ", card2)
# numbers 1 - 10
if card2 in ["2", "3", "4", "5", "6", "7", "8", "9", "10"]:
cardnum2 = int(card2)
# special cards
elif card2 == "Jack":
cardnum2 = int(10)
elif card2 == "Queen":
cardnum2 = int(10)
elif card2 == "King":
cardnum2 = int(10)
elif card2 == "Ace":
cardnum2 = input("do you want your Ace to be a 1 or a 11?")
print("your cards combined are ", cardnum1 + cardnum2)
答案 3 :(得分:0)
虽然其他人给出了您的程序的工作版本,并指出了您的版本存在的问题,但让我为您提供一个简单的选择:
import random
deck = ['Ace', 2, 3, 4, 5, 6, 7, 8, 9, 'Jack', 'Queen', 'King']
card1 = random.choice(deck)
card2 = random.choice(deck)
def card_value(card):
if card == 'Ace':
chosen_value = input("do you want your Ace to be a 1 or a 11?")
return int(chosen_value)
elif card in ['Jack', 'Queen', 'King']:
return 10
else:
# Return the integer as it is
return card
print("Your first card is", card1)
print("Your first card is", card2)
print("Your cards combined are ", card_value(card1) + card_value(card2))
此版本与您的版本之间唯一的主要区别是,我使用card_value
帮助方法来将卡的值导出为整数。如果卡值已经是整数,则照原样返回。
PS:
欢迎来到编程世界,这很酷,您现在就尝试挑战自己。不必担心您的问题会否决票;它发生了。但是您可以通过有条不紊地解决您的问题来避免这种情况:
在一开始,您可能会发现,花时间并学习像程序员一样思考,比深入研究任何一种语言都更有益。查看这些链接以了解我的意思: