【发布时间】:2015-06-28 14:32:13
【问题描述】:
这个程序应该玩纸牌游戏战争。好的,所以我需要能够提示用户继续玩游戏并让他们通过按 Enter 键来响应。我不确定如何做到这一点。帮忙?
import cards
# Create a deck of cards
the_deck = cards.Deck()
# Shuffle the deck, then display it in 13 columns
the_deck.shuffle()
print( "===== shuffled deck =====" )
the_deck.display()
def main():
'''This function deals out half the deck to each
player. It sorts the rank so as to solve the problem
with Aces causing an infinite game'''
player1_list=[]
player2_list=[]
for i in range( 26 ):
p1_temp= the_deck.deal()
player1_list.append( p1_temp )
p2_temp= the_deck.deal()
if (p2_temp.rank()==1):
player1_list.append(p2_temp)
player2_list.append(player1_list.pop(0))
else:
player2_list.append(p2_temp)
print()
# Card dealt to Player #1
player1_card = player1_list.pop( 0 )
print( "===== player #1 =====" )
print( "Card dealt to player #1:", player1_card )
print( player1_list )
print()
#Card dealt to Player #2
player2_card=player2_list.pop(0)
print( "===== player #2 =====" )
print("Card dealt to player #2:",player2_card)
print( player2_list )
# Compare the two cards using overloaded operators
print()
if player1_card == player2_card:
print( "Tie:", player1_card, "and", player2_card, "of equal rank" )
elif player1_card > player2_card:
print("Player #1 wins:",player1_card,"of higher rank than",player2_card)
else:
print("Player #2 wins:",player2_card,"of higher rank than",player1_card)
print()
main()
def keep_playing():
'''Determines whether the player wants to continue. If so they press the
Enter key and the function calls main to start all over.'''
still_playing=input('Press "Enter" to continue playing')
Enter=1
while still_playing==Enter:
main()
keep_playing()
【问题讨论】:
-
为什么会运行?你不会在任何地方调用它。
标签: python python-3.x war