In: Computer Science
Can I have this answer in a screenshot of the python software?
In the game of Lucky Sevens, the player rolls a pair of dice. If the dots add up to 7, the player wins $4; otherwise, the player loses $1. Suppose that, to entice the gullible, a casino tells players that there are many ways to win: (1, 6), (2, 5), and soon. A little mathematical analysis reveals that there are not enough ways to win to make the game worthwhile; however, because many people's eyes glaze over at the first mention of mathematics “wins $4”.
Your challenge is to write a program that demonstrates the futility of playing the game. Your Python program should take as input the amount of money that the player wants to put into the pot, and play the game until the pot is empty.
The program should have at least TWO functions (Input validation and Sum of the dots of user’s two dice). Like the program 1, your code should be user-friendly and able to handle all possible user input. The game should be able to allow a user to ply as many times as she/he wants.
The program should print a table as following:
Number of rolls Win or Loss Current value of the pot
1 Put $10
2 Win $14
3 Loss $11
4
## Loss $0
You lost your money after ## rolls of play.
The maximum amount of money in the pot during the playing is $##.
Do you want to play again?
At that point the player’s pot is empty (the Current value of the pot is zero), the program should display the number of rolls it took to break the player, as well as maximum amount of money in the pot during the playing.
Again, add good comments to your program.
Test your program with $5, $10 and $20.
import random
def validate_input(money):
# If money is 0, return false else true
if money <= 0:
return False
else:
return True
def roll_dice():
# Roll dice twice and return their sum
dice1 = random.randint(1, 6)
dice2 = random.randint(1, 6)
return dice1 + dice2
if __name__ == '__main__':
choice = 'y'
# Iterate loop till choice is y or Y
while choice.lower() == 'y':
# Ask user to enter money they want to put
# Validate input. If false, print invalid input else continue with program
money = int(input('Enter the amount of money you want to put in the pot: '))
if validate_input(money) is False:
print('Invalid input!')
else:
# Print column header
print("\n%-20s%-20s%-20s" % ("Number of rolls", "Win or Loss", "Current value of the pot"))
# Set, rolls = 1, win_or_loss = "Put" and maximum_money = money intially
# Print the values
rolls = 1
win_or_loss = "Put"
maximum_money = money
print("%-20d%-20s$%-20d" % (rolls, win_or_loss, money))
# Iterate while loop till money > 0
# call function and
# if returned value is = 7, set win_or_loss to Win and add 4 to money
# else, set win_or_loss to Loss and subtract 1 from money
while money > 0:
if roll_dice() == 7:
win_or_loss = "Win"
money += 4
else:
money -= 1
win_or_loss = "Loss"
# Increment rolls by 1
rolls += 1
# If maximum_money < money, set maximum_money = money
if maximum_money < money:
maximum_money = money
# Print current values
print("%-20d%-20s$%-20d" % (rolls, win_or_loss, money))
# After the inner loop, print total rolls and maximum_money
print('\nYou lost your money after %d rolls of play.' % (rolls))
print('The maximum amount of money in the pot during the playing is $%d' % (maximum_money))
# Ask user if they want to play again
choice = input('\nDo you want to play again? (y/n) ')
print()
SCREENSHOT



OUTPUT





