|
| 1 | +# RANDOM TEST of '\n' concatinated to the file.write # |
| 2 | + |
| 3 | +# def get_greeting(name): |
| 4 | +# return f'Good evening {name}' |
| 5 | +# message = get_greeting('Eric') |
| 6 | +# file = open('get_greeting.txt', 'a') |
| 7 | + |
| 8 | +# file.write(message + '\n') |
| 9 | +# file.close() |
| 10 | + |
| 11 | +# try: |
| 12 | +# num = int(input('Enter a number: ')) |
| 13 | +# divide_num = 100/num |
| 14 | +# except (ValueError, ZeroDivisionError): |
| 15 | +# print('Please enter a number other than zero') |
| 16 | +# # print(ex, 'printing the ex error') |
| 17 | +# # print(type(ex)) |
| 18 | +# # except ZeroDivisionError: |
| 19 | +# # print('Numbers cannot = 0') |
| 20 | +# else: |
| 21 | +# print('We are printing the else clause') |
| 22 | + |
| 23 | +# message = input('Enter a number: ') |
| 24 | + |
| 25 | +# try: |
| 26 | +# numbers = float(message) |
| 27 | +# except ValueError: |
| 28 | +# print('Please enter a number') |
| 29 | +# else: |
| 30 | +# print('Your number converted successfully') |
| 31 | +# finally: |
| 32 | +# print('Finally is printing') |
| 33 | + |
| 34 | +# names = ['Peter', 'Parker'] |
| 35 | + |
| 36 | +# while True: |
| 37 | +# message = input('Enter a number:') |
| 38 | +# try: |
| 39 | +# number = int(message) |
| 40 | +# names[number] #names[4] creates an out of index range error |
| 41 | +# break |
| 42 | +# except(ValueError, IndexError) as error: |
| 43 | +# if type(error) == IndexError: |
| 44 | +# print('We have an index error') |
| 45 | +# else: |
| 46 | +# print('Pleaes enter a valid number') |
| 47 | +# # print(error, 'Printing except error') |
| 48 | + |
| 49 | +# print('No error has occured') |
| 50 | + |
| 51 | + |
| 52 | +# Lab 5: Pick6 |
| 53 | + |
| 54 | +# Have the computer play pick6 many times and determine net balance. |
| 55 | + |
| 56 | +# Initially the program will pick 6 random numbers as the 'winner'. Then try playing pick6 100,000 times, with the ticket cost and payoff below. |
| 57 | + |
| 58 | +# A ticket contains 6 numbers, 1 to 99, and the number of matches between the ticket and the winning numbers determines the payoff. |
| 59 | +# Order matters, if the winning numbers are [5, 10] and your ticket numbers are [10, 5] you have 0 matches. |
| 60 | +# If the winning numbers are [5, 10, 2] and your ticket numbers are [10, 5, 2], you have 1 match. Calculate your net winnings (the sum of all expenses and earnings). |
| 61 | + |
| 62 | +# a ticket costs $2 |
| 63 | +# if 1 number matches, you win $4 |
| 64 | +# if 2 numbers match, you win $7 |
| 65 | +# if 3 numbers match, you win $100 |
| 66 | +# if 4 numbers match, you win $50,000 |
| 67 | +# if 5 numbers match, you win $1,000,000 |
| 68 | +# if 6 numbers match, you win $25,000,000 |
| 69 | + |
| 70 | +# One function you might write is pick6() which will generate a list of 6 random numbers, which can then be used for both the winning numbers and tickets. |
| 71 | +# Another function could be num_matches(winning, ticket) which returns the number of matches between the winning numbers and the ticket. |
| 72 | +# Steps |
| 73 | + |
| 74 | +# Generate a list of 6 random numbers representing the winning tickets |
| 75 | +# Start your balance at 0 |
| 76 | +# Loop 100,000 times, for each loop: |
| 77 | +# Generate a list of 6 random numbers representing the ticket |
| 78 | +# Subtract 2 from your balance (you bought a ticket) |
| 79 | +# Find how many numbers match |
| 80 | +# Add to your balance the winnings from your matches |
| 81 | +# After the loop, print the final balance |
| 82 | + |
| 83 | + |
| 84 | +# VERSION 1 # |
| 85 | +import random |
| 86 | + |
| 87 | + |
| 88 | +def num_matches(winning, ticket): |
| 89 | + matches = 0 |
| 90 | + for number in range(6): |
| 91 | + # print(winning[number], ticket[number]) |
| 92 | + |
| 93 | + if winning[number] == ticket[number]: |
| 94 | + # print("YOU WIN!") |
| 95 | + matches += 1 |
| 96 | + return matches |
| 97 | + |
| 98 | + |
| 99 | +# Generate a list of 6 random numbers representing the winning tickets |
| 100 | + |
| 101 | + |
| 102 | +def pick6(): |
| 103 | + ticket_numbers = [] |
| 104 | + for x in range(6): |
| 105 | + # print(x) |
| 106 | + ticket_numbers.append(random.randint(1, 99)) |
| 107 | + # print(ticket_numbers) |
| 108 | + return ticket_numbers |
| 109 | + |
| 110 | + |
| 111 | +winning_ticket = pick6() |
| 112 | +earnings = 0 |
| 113 | +runs = 100000 |
| 114 | +expenses = runs * 2 |
| 115 | + |
| 116 | +for x in range(runs): |
| 117 | + |
| 118 | + match_counter = 0 |
| 119 | + |
| 120 | + match_counter += num_matches(winning_ticket, pick6()) |
| 121 | + |
| 122 | + if match_counter == 1: |
| 123 | + earnings += 4 |
| 124 | + elif match_counter == 2: |
| 125 | + earnings += 7 |
| 126 | + elif match_counter == 3: |
| 127 | + earnings += 100 |
| 128 | + elif match_counter == 4: |
| 129 | + earnings += 50000 |
| 130 | + elif match_counter == 5: |
| 131 | + earnings += 1000000 |
| 132 | + elif match_counter == 6: |
| 133 | + earnings += 25000000 |
| 134 | + |
| 135 | +payout = earnings - expenses |
| 136 | + |
| 137 | +print(payout) |
| 138 | + |
| 139 | + |
| 140 | +# VERSION 2 # |
| 141 | + |
| 142 | + |
| 143 | +roi = (earnings - expenses) / expenses |
| 144 | + |
| 145 | + |
| 146 | +print(roi) |
0 commit comments