如何使这种添加方法更简单?

时间:2016-11-29 13:06:23

标签: python

此代码添加了一组卡片(在二十一点游戏中)

# Rules
print("Dont enter Ace, Jack, Queen or king in the first 4 Inputs")
print("You will be told when to enter those")
print("if you dont have a card just enter 0")

# Line break
print("")

card1 = int(input("What is your first card: "))
card2 = int(input("What is your second card: "))
card3 = int(input("What is your third card: "))
card4 = int(input("What is your fourth card: "))


# Line break
print("")

# print("King = 10 \nJack = 10 \nQueen = 10 \nAce = 11 or 1 ")
AJKQ = (input("Any Ace, jack, Queens or kings: "))
AJKQ2 = (input("Any Ace, jack, Queens or kings: "))

# Line Break
print("")

print(card1 + card2 + card3 + card4 + AJKQ + AJKQ2)

代码需要添加用户拥有的卡并输出Ace,jack queen和king

我对ifs和elifs也不是很好,所以最好的方法是将它压缩成更小的代码吗?

2 个答案:

答案 0 :(得分:0)

cards = ['first', 'second', 'third', 'fourth']
card = []
for value in cards:
   card.append(int(input("What is your "+value+" card: ")))

...

print("".join(card) + AJKQ + AJKQ2)

答案 1 :(得分:0)

print("""Dont enter Ace, Jack, Queen or king in the first 4 Inputs
You will be told when to enter those.
if you dont have a card just enter 0\n""")

print(sum([int(input("what is your "+ char +" card: ")) for char in("first", "second", "third","fourth")]) +
sum([int(input("Any Ace, jack, Queens or kings: ")) for i in range(0,2)]))

这应该没问题。