In: Computer Science
Instructions
Use your solution from Programming Assignment 5 (or use your instructor's solution) to create a modular Python application. Include a main function (and a top-level scope check),and at least one other non-trivial function (e.g., a function that deals the new card out of the deck). The main function should contain the loop which checks if the user wants to continue.
Include a shebang, and ID header, descriptive comments, and use constants where appropriate.
Sample Output (user input in red):
Welcome to the card dealer!
Here is your first card:
5 of hearts
Would you like another card? (y or n) y
King of hearts
Would you like another card? (y or n) y
Jack of clubs
Would you like another card? (y or n) y
King of spades
Would you like another card? (y or n) n
final hand value = 35
final hand: ['5 of hearts', 'King of hearts', 'Jack of clubs',
'King of spades']
Submission
Attach and submit your Python file to this assignment.
Python3 code:
import random
suits = ["clubs", "diamonds", "hearts", "spades"]
chars = ["ace", "king", "queen", "jack"]
total = random.randint(1,9)
print("Welcome to the card dealer!")
print("Here is your first card:")
temp = random.randint(0,3)
hand = []
hand.append(str(total) + " of " + suits[temp])
print(total, "of", suits[temp])
while(True):
print("Would you like to add another card? (y/n)")
inp = input().lower()
if(inp=="y"):
s = input().strip().split()
if(len(s)==3 and s[1]=="of" and (s[2].lower() in suits)):
#check for number from 1 to 9
if(s[0].isdigit() and int(s[0])>=1 and int(s[0]) <=9 ):
total+=int(s[0])
hand.append(str(s[0]) + " of "+ s[2])
#check for "ace", "king", "queen"
elif(s[0].lower() in chars):
total+=10
hand.append(s[0] + " of " + s[2])
else:
print("Wrong input!")
else:
print("Wrong input!")
elif(inp=="n"):
print("final hand value =",total)
print("final hand: ",hand)
break
else:
print("Wrong input. Please enter y or n")