简体   繁体   English

Python:如何漂亮地打印卡片

[英]Python: How to pretty print the cards

I just started learning python and then this card game came out and I had no idea what to do.我刚开始学习 python 然后这个纸牌游戏出来了,我不知道该怎么办。 Basically, I need to create a for-loop inside of which I ask the user for card:.基本上,我需要创建一个 for 循环,在其中我向用户询问卡片:。 Most importantly, I need to define a function outside of main(), like def ppcard( card ):, and prints out the "pretty-print" version of that card.最重要的是,我需要在 main() 之外定义一个 function,如 def ppcard(card):,并打印出该卡的“漂亮打印”版本。 If a user put card: 4h, the output should be 4 of Spade.如果用户放卡:4h,output 应该是 4 的 Spade。 So that's where I got confused and I tried lots of times but I don't know how I can do the pretty print.所以这就是我感到困惑的地方,我尝试了很多次,但我不知道如何才能做出漂亮的印刷品。 I don't know how to match the values in deck like the 's' in '2s' to 'Spade'.我不知道如何将甲板中的值(如“2s”中的“s”与“Spade”匹配)。

You can use the respective letter in card as keys for the ranks and suits dicts:您可以使用card中的相应字母作为rankssuits字典的键:

def ppcard(card):
    print(ranks[card[0]], 'of', suits[card[1]])

Also note that you're missing quotes around the strings for the keys of the suits dict:另请注意,您缺少suits字典键的字符串周围的引号:

suits = {"s": "Spades", "h": "Hearts", "d": "Diamonds", "c": "Clubs"}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM