Header Ads

You're writing 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.

 Code :

def blackjack(a, b):
s = a + b
if s > 21:
if a == 11:
a = 1
elif b == 11:
b = 1
else:
a, b = 0, 0
s = a + b
return s


i = int(input("Enter First Card Number : "))
j = int(input("Enter Second Card Number : "))

print("Sum =", blackjack(i, j))
Functions is set of instructions which perform specific task.
Functions are of two types:
1. Built-in functions
2. User-defined functions
def is used to define User-defined function in python. Functions provide ease to the programmer and avoid repetition of code. In above code blackjack is a user-defined function.
Pre-defined functions are known as built-in functions. In above code input is a built-in function which is used to take input from the user.

Output :

when i = 11 and j = 8
Enter First Card Number :  11
Enter Second Card Number :  13
Sum = 14

when i = 12 and j = 14
Enter First Card Number : 12
Enter Second Card Number : 14
Sum = 0

Post a Comment

0 Comments