Ich versuche ein Programm zu schreiben, das Funktionen verwendet, um eine Datei zu öffnen und dann die Zeilen in der Datei zu verwenden, um ein Kartenspiel zu spielen, aber ich bekomme weiter dieser Fehler. TypeError: Coercing zu Unicode: brauche String oder Puffer, Typ gefunden, Funktionen
Traceback (most recent call last): File "/home/ecunning/Documents/Treasure Hunt Game Lab", line 115, in drawCard() File "/home/ecunning/Documents/Treasure Hunt Game Lab", line 102, in drawCard deck = readDeck(file) File "/home/ecunning/Documents/Treasure Hunt Game Lab", line 26, in readDeck infile = open(fileName , 'r') TypeError: coercing to Unicode: need string or buffer, type found
Hier ist mein Code:
def getfileName():
fileName = raw_input("What is the name of the file?: ")
return fileName
while (fileName != fileDescription):
print("Invalid file name. Select an appropriate file: ")
fileName = raw_input("What is the name of the file?: ")
return fileName
def readDeck(fileName):
infile = open(fileName , 'r')
deck = []
for card in infile:
print card
return deck
def getCardType(deck):
cardType = input("What is the location of the card would you like to extract from the deck: ")
card = deck[cardType]
return card
print card
while (cardType > len(deck)):
print "This location is not in the deck. Re-enter a location that is within the list"
cardType = input("What is the location of the card would you like to extract from the deck: ")
card = deck[cardType]
return card
print card
newDeck = deck.remove(str(card))
cardGroup = card[:1]
return cardGroup
print cardGroup
if str(cardGroup) == ruby:
print "This card is a ruby"
elif str(cardGroup) == emerald:
print "This card is an emerald"
elif str(cardGroup) == coal:
print "This card is coal"
else:
print "This card is a diamond"
def getCardValue(card):
cardValue = card[2:]
cardAmount = int(cardValue)
return cardAmount
def displayList(card):
drawnCards = []
drawnCards.append(card)
drawnCards.sort()
print ("List of drawn cards: ") ,drawnCards.sort()
def getPosition():
cardType = input("What is the location of the card would you like to extract from the deck: ")
card = newDeck[cardType]
return card
print card
while (cardType > len(deck)):
print "This location is not in the deck. Re-enter a location that is within the list"
cardType = input("What is the location of the card would you like to extract from the deck: ")
card = deck[cardType]
return card
print card
def drawCard():
cardtotal = 0
while (cardtotal < winValue):
getfileName()
deck = readDeck(file)
cardType = getCardType(deck)
if (str(cardGroup) != coal):
cardtotal = cardtotal + cardAmount
print cardtotal
getCardValue(card)
displayList(card)
cardType = getPosition()
else:
print "You drew a coal card. The game is over"
print "You have drawn 21 points! You win!"
drawCard()
Jede Hilfe wäre toll !! Vielen Dank.
Sie haben mehrere Probleme, die aktuelle ist 'deck = readDeck (Datei)' Sie übergeben 'Datei', die eine Python-Klasse/Typ ist, sollte ich mir vorstellen, ' readDeck (getfileName()) '. Ihre while-Schleife nach 'return fileName' ist nicht erreichbar und Sie scheinen auf mehrere undefinierte Variablen zuzugreifen –