在写21点脚本时,我遇到了一些关于如何使用“if”、“elif”和“else”语句的困惑。我看了很多关于这个主题的帖子,在谷歌上搜索了一下,但还是很困惑。我确实了解到,如果使用“elif”而不是重复“if”语句,则当“elif”语句(或其中一个语句)的计算结果为True时,代码将短路。这实际上让我更加困惑(虽然我理解使用“elif”和短路时会发生什么的概念)。前5个“if”语句说明了这一点。如果我用了“elif”而不是“if”,如果玩家和庄家都达到了21,代码可能永远不会达到最后一个条件。不过,在这之后,我似乎可以使用“elif”语句,或者干脆就这样。所以,我的问题是,我在main()的其余部分中是否正确地使用了它们?如果没有,你会怎么做?非常感谢你。在
# current working version - - - 02/26/2013
# Notes: Nees to fix Ace problem. Ace can be 11 or 1.
import random
import os
def main():
print "Welcome To Python Blackjack. [H] Is For A Hit, [S] Is To Stand, [Q] To Quit.\n"
c = "" # Hit, Stand or Quit Variable.
player = deal_cards() # deal player
dealer = deal_cards() # deal dealer
print "< ---- Player Hand ---->"
print "Player Hand: ", player
print "Total Player Hand: ", total_hand(player)
print
print "< ---- Dealer Hand ---->"
print "Dealer Hand: ", dealer
print "Total Dealer Hand: ", total_hand(dealer)
print
if (total_hand(player) == 21):
print "BLACKJACK! YOU WIN!"
message()
if (total_hand(player) > 21):
print "BUSTED! You Lose"
message()
if (total_hand(dealer) == 21):
print "BLACKJACK! Sorry You Lose! Dealer Wins" # must use if statements because elif would fail to reach the tie line.
message()
if (total_hand(dealer) > 21):
print "Dealer Busted! You Win!"
message()
if (total_hand(player) == 21) and (total_hand(dealer) == 21): # must use if statements because elif would fail to reach this line.
print "Player And Dealer Tie! Game Goes To Dealer"
message()
while (c != "q"):
c = raw_input("[H]it [S]tand [Q]uit: ").lower()
if (c == "h"):
hit(player)
print ""
print "Your Cards Are Now: ",player
print "Total For Player Is: ",total_hand(player)
if (total_hand(player) == 21):
print "BLACKJACK! You Win!"
message()
if (total_hand(player) > 21):
print "BUSTED! Sorry, You Lose."
message()
if (total_hand(dealer) == 21):
print "BLACKJACK! Sorry You Lose! Dealer Wins."
message()
if (total_hand(dealer) > 21):
print "Dealer Busted! You Win!\n"
message()
if (total_hand(dealer) <= 17):
hit(dealer)
print "\nThe Dealer Takes A Card", dealer
print "For A Total Of: ", total_hand(dealer)
if (total_hand(dealer) == 21):
print "BLACKJACK! Sorry You Lose! Dealer Wins.\n"
message()
if (total_hand(dealer) > 21):
print "Dealer Busted! You Win!\n"
message()
elif (c == "s"):
if (total_hand(dealer) <= 17):
hit(dealer)
print "The Dealer Takes A Card", dealer
print "For A Total Of: ", total_hand(dealer)
if (total_hand(dealer) == 21):
print "BLACKJACK! Dealer Wins.\n"
message()
if (total_hand(dealer) > 21):
print "Dealer Busted! You Win!\n"
message()
if (total_hand(dealer) >= total_hand(player)):
print "Sorry, You Lose. Dealer Wins With A Tie\n"
message()
if (total_hand(player) > total_hand(dealer)):
print "You Win With The Best Hand!\n"
message()
if (total_hand(player) > total_hand(dealer)):
print "You Win With The Best Hand!\n"
message()
if (total_hand(dealer) > total_hand(player)):
print "Sorry, You Lose. Dealer Wins\n"
message()
else:
if (c == "q"):
message()
else:
print "Invalid Choice. . .To Quit, Press [Q]"
def deal_cards():
random1 = random.randint(1,11)
random2 = random.randint(1,11)
hand = [random1, random2]
return hand
def hit(hand):
newCard = random.randint(1,11)
hand.append(newCard)
return hand
def total_hand(hand):
total = sum(hand)
return total
def message():
again = raw_input("Do You Want To Play Again? [Y] For Yes - Press Any Key To Quit: ").lower()
if "y" in again:
main()
else:
print "Thanks For Playing"
os._exit(1)
# main
if __name__ == '__main__':
main()
试着把更具体的条件放在不太具体的条件之前。在
例如,如果您从
为了这个
^{pr2}$因为满足最后一个语句所需的条件对于第一个或第三个语句来说是真的,所以您不能用elifs达到语句的所有条件。在
你的代码的一个主要问题是你应该先检查平局,否则你就宣布你的球员是赢家。在
对于您的代码,使用
if
还是elif
,实际上并没有什么区别。这是因为message()
函数从未实际返回:它要么退出程序,要么递归地调用main()
。这不是一个好的设计:任何其他阅读您的代码的人都不会期望一个名为message()
的函数来完成这两件事。在我的建议是创建一个函数来检查游戏是否结束,并返回一个描述结果的字符串。我可以这样做;但是请注意,即使在这里也可以使用},因为
if
而不是{return
语句无论如何都会退出函数。在不,有几个小的缩进错误,最后,您可以使用elif语句。下面是您的代码应该是什么样子的。在
相关问题 更多 >
编程相关推荐