Dcoder icon

The BlackJack with conditions. @ Dcoder

By Lochard | As a newbie programmer | 21 Jul 2023


Problem: Write a program to play a variety of BlackJack. In general, given two numbers, a and b, return their sum.

If the sum is greater than 21, return 0, unless one of the numbers is 11. In such a case, the 11 should be 'converted' to a 1 to prevent the sum from being exceeded.

For example, given a 11 and 13 as input, the 11 should be 'converted' into a 1 so the total sum will be 14.

Input: Input contains 2 space separated integers, a and b

Output: Output will be in general sum of a and b, if sum exceed 21, return 0, unless one of the numbers is 11. In such a case, the 11 should be 'converted' to a 1 to prevent the sum from being exceeded.

For example, given a 11 and 13 as input, the 11 should be 'converted' into a 1 so the total sum will be 14.

Constraints: 0<=a<50

 0<=b<50

Sample Input: 11 13

Sample Output: 

14

a, b = map(int, input().split())
s = a + b
if s > 21 and 11 in (a, b):
    s -= 10
print(s if s < 22 else 0)

How do you rate this article?

1


Lochard
Lochard

20240228 Arrived in the UK for about 2 week. All my fears persist. Some are even getting worse. https://github.com/locharp/asylum_diary/


As a newbie programmer
As a newbie programmer

Sharing entry level codes. My snippets on GitHub https://github.com/locharp/code-snippets

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.