【发布时间】:2015-06-08 09:18:01
【问题描述】:
这是我修改后的二十一点。
我还是 Python 新手,所以这个程序中缺少格式和严重的规则修改,但我不明白的是,如果你要运行这个程序,为什么 Total1 的输出与 @ 相同987654322@.
在第 1 阶段,如果您只是通过并确认您通过了,那么一切都会顺利进行。您有自己的总数,即Card1 和Card2 的总和,庄家有自己的总数(我作弊并让他击中3 次):Card1、Card2、Card3 .所以PHASE 1 一切正常。
但是当我输入PHASE 2 时,执行相同的过程(通过和确认)会使Total1 和DealerTotal 输出相同。
为什么PHASE 1 中的DealerTotal 可以工作,而PHASE 2 中的DealerTotal 即使代码相同也不行?
# Imports
import random
import sys
# Dictionary
Cards = {"1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9,
"10": 10, 'Jack': 10, 'Queen': 10, 'King': 10, 'Ace': 10}
# Welcome Print
print('Welcome to Blackjack!\n\nHere are your cards: \n ')
# Cards
keys = list(Cards)
# Beginning Cards
Card1 = random.choice(keys)
Card2 = random.choice(keys)
# Hit 1 Card
Card3 = random.choice(keys)
# Hit 2 Card
Card4 = random.choice(keys)
# Hit 3 Card
Card5 = random.choice(keys)
# Hit 4 Card
Card6 = random.choice(keys)
# Hit 5 Card
Card7 = random.choice(keys)
# Hit 6 Card
Card8 = random.choice(keys)
# Sum of Cards
# Beginning Total
Total = Cards[Card1] + Cards[Card2]
# Hit 1 Total
Total1 = Total + Cards[Card3]
# Hit 2 Total
Total2 = Total1 + Cards[Card4]
# Hit 3 Total
Total3 = Total2 + Cards[Card5]
# Hit 4 Total
Total4 = Total3 + Cards[Card6]
# Dealer's Total
DealerTotal = Cards[Card1] + Cards[Card2] + Cards[Card3]
### BEGINNING ###
print(Card1, 'and a', Card2, '. Your total is', Total)
### PHASE 1 ###
Choice = input('Would you like to hit or pass? (h = hit, p = pass): ')
# Hitting PHASE 1
if (Choice == 'h'):
print('The dealer gives you a', Card3, '. Your new total is: ' , Total1)
# Hitting & Losing HIT 1
if (Total1 > 21):
print('You lose! Try again later.')
sys.exit()
# Passing PHASE 1
if (Choice == 'p'):
# Confirmation whether you want to pass/hit
Response1 = input('Are you sure? The dealer may have a better hand. (y = yes, n = no): ')
# Reponse to hitting 'y'
if (Response1 == 'y'):
print('Your total is', Total, "The dealer's total is", DealerTotal)
if (DealerTotal > 21 or 21 >= Total > DealerTotal):
print("Congratulations, you've won!")
sys.exit()
elif (DealerTotal == Total):
print('You have tied!')
else:
print('You lose! Try again later.')
sys.exit()
### PHASE 2 ###
Choice1 = input('Would you like to hit or pass? (h = hit, p = pass): ')
# Hitting PHASE 2
if (Choice1 == 'h'):
print('The dealer gives you a', Card4, '. Your new total is: ' , Total2)
# Hittnig & Losing HIT 2
if (Total2 > 21):
print('You lose! Try again later.')
sys.exit()
# Passing PHASE 2
if (Choice1 == 'p'):
# Confirmation whether you want to pass/hit
Response2 = input('Are you sure? The dealer may have a better hand.(y = yes, n = no): ')
# Reponse to hitting 'y'
if (Response2 == 'y'):
print('Your total is', Total1, "The dealer's total is", DealerTotal)
if (DealerTotal > 21 or 21 >= Total1 > DealerTotal):
print("Congratulations, you've won!")
sys.exit()
elif (DealerTotal == Total1):
print('You have tied!')
else:
print('You lose! Try again later.')
sys.exit()
【问题讨论】:
-
你有
Total = Cards[Card1] + Cards[Card2]; Total1 = Total + Cards[Card3]和DealerTotal = Cards[Card1] + Cards[Card2] + Cards[Card3]。基本上,你有x = a + b; x = x + c和y = a + b + c。为什么您希望它们有所不同?
标签: python variables blackjack