你好,我是悦创。

英国一对一学员Python扑克牌作业答疑_python

英国一对一学员Python扑克牌作业答疑_python_02

代码:https://www.aiycoj.cn/?id=bc5e0e0d-d732-4a46-a2b8-8449f624542e

作业内容

编写一个程序,接受用户的输入,描述一张扑克牌(使用下面的速记符号 Short-hand),并打印出 Description。

英国一对一学员Python扑克牌作业答疑_3d_03

要求


  • 用户输入只能是 2 或者 3 个字符,不要空格。例如 10H,3D。
  • 第一个字符(或者头两个字符,如果卡牌为 10)要在 Ranks 里,最后一个字符要在 Suits 里。
  • 无论用户输入大小写字母,程序都应该能顺利运行
  • 如果用户输入了不合规格的字符或者字符不存在与 Ranks 和 Suits,则应该报 Invalid Card Entered。

Input & Output 的例子

英国一对一学员Python扑克牌作业答疑_python_04

她写的代码是对的,所以我就直接放她写的代码:

shorthand = input("Enter the card notation: ").upper()
length = len(shorthand)
ranks = {'1': 'One', '2': 'Two', '3': 'Three', '4': 'Four', '5': 'Five', '6': 'Six', '7': 'Seven', '8': 'Eight', '9': 'Nine', 'A': 'Ace', 'J': 'Jack', 'Q': 'Queens', 'K': 'King'}
suits = {'H': 'Hearts', 'D': 'Diamond', 'S': 'Spades', 'C': 'Clubs'}

if length == 3:
if shorthand[0:2] == '10' and shorthand[-1] in suits:
print('Ten of ' + suits[shorthand[-1]])

else:
print('Invalid Card Entered')

elif length == 2:

if shorthand[0] in ranks and shorthand[-1] in suits:
print(ranks[shorthand[0]] + ' of ' + suits[shorthand[-1]])

else:
print('Invalid Card Entered')

else:
print('Invalid Card Entered')

英国一对一学员Python扑克牌作业答疑_后端_05

好,就到这里。下课!