2016-11-29 78 views
0

該代碼添加一組牌(在遊戲的21點)我怎樣才能使這種加法更簡單?

# 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) 

代碼需要加起來得到了用戶的卡,並與王牌輸出插孔王后和國王

林還ifs和elifs不是很好,那麼最好的辦法是將它壓縮成更小的代碼?

+4

如果你的代碼正在工作,這可能是在這裏的主題。嘗試提交到https://codereview.stackexchange.com/。 – Chris

+2

爲什麼讓你的用戶按下「Enter」這麼多次? 'cards = input('輸入所有的卡片,並在它們之間留有空格')。split()**沒有人喜歡與電腦進行擴展對話。 – FMc

+0

@FMc - 如果輸入是全部數字,則按下Enter鍵而不是空格鍵可能會更容易,因爲許多鍵盤上的數字鍵盤上都有一個Enter鍵。 – TigerhawkT3

回答

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)])) 

這應該是罰款。

+0

謝謝,這很有幫助, – Cyqix

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) 
+0

雖然這可能是一個有效的答案,但通過解釋代碼的作用和工作原理,您更有可能幫助其他人。僅有代碼的答案往往會得到較少的積極關注,並沒有其他答案那麼有用。 – Aurora0001