[英]Python 2.7.3 - Function not returning the right values in a string formatted table
這是我的Black Jack游戲的當前代碼:
import random
def showMenu():
userInput = raw_input("Welcome to the game of Black Jack! Please choose an option from the following: \n - Start Game \n - Rules \n - Exit")
if userInput == "Start Game":
return maingame()
def maingame():
done = False
return cardGenerator()
print "{0:>4} {01:>18} {02:>20} {03:>18}".format("Player Money", (cards[0], cards[1]), "CPU Cards", "CPU Money")
def getInitialMoney():
initialdough = 5000
def cardGenerator():
#Assign a random suit
suit_card = ["Hearts", "Spades", "Clubs", "Diamond"]
from random import choice
#Assign a random number between 1-13 (Ace to King)
number_card = random.randrange(1,14)
cards = choice(suit_card), (number_card)
def getDecision():
getDecision = raw_input("What will you do? \n - Hit \n - Stand")
if getDecision == "Hit":
return hit()
elif getDecision == "Stand":
return stand()
def hit():
return cardGenerator()
def stand():
return opponentphase()
def raise_bet():
raise_amount = input("How much will you bet?")
total_pot = 0 + raise_amount
def main():
maingame()
main()
問題出在maingame()中。 返回cardGenerator有效調用該函數嗎? 不知道為什么我不能從字符串格式的列表中為一個值編制索引,直到我現在運行時它什么都沒有返回。
抱歉,如果事情不清楚,我真的很難說清楚我要解釋的內容
在方法maingame()
,您將返回cardGenerator()
的結果cardGenerator()
由於未返回任何內容,因此返回None
),因此您的方法maingame()
永遠不會到達print
語句。 我認為您可以進行以下修改:
def maingame():
done = False
cards = cardGenerator()
print "{0:>4} {01:>18} {02:>20} {03:>18}".format("Player Money", (cards[0], cards[1]), "CPU Cards", "CPU Money")
並在cardGenerator()
返回 cards
,因此您可以在maingame()
使用它
def cardGenerator():
#Assign a random suit
suit_card = ["Hearts", "Spades", "Clubs", "Diamond"]
from random import choice
#Assign a random number between 1-13 (Ace to King)
number_card = random.randrange(1, 14)
cards = choice(suit_card), (number_card)
return cards
請注意,您從未聲明過opponentphase()
方法(至少它不在您發布的代碼中)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.