2016-04-17 8 views
-1

ファイルを開いてファイル内の行を使用してカードゲームを行う関数を使用するプログラムを作成しようとしていますが、このエラー。ここでTypeError:Unicodeに強制する:必要な文字列またはバッファ、型が見つかった、関数を呼び出す

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

は私のコードです:

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() 

すべてのヘルプは素晴らしいことです!ありがとう。

+0

あなたは複数の問題があります。現在の問題は、 'file ='ファイルを渡している 'deck = readDeck(file)'です。 readDeck(getfileName()) 'です。 'return fileName'の後のあなたのwhileループは到達可能ではなく、複数の未定義の変数にアクセスしているようです –

答えて

1

stringオブジェクトの代わりにfunctionオブジェクトをその行に渡しているため、このエラーが発生しています。私はあなたがやろうとしているか想像したい、このようにそのメソッドを呼び出している:

deck = readDeck(getfileName()) 

また、あなたのプログラムの中で変数のスコープと制御フローとの数多くの問題を抱えているように見えるので、私はあなたにread up onyour problemsをお勧めしてから行きますあなたのプログラムを通して、あなたが見るすべての問題を修正してください。

関連する問題