Computer Programing Python - Midterm
Computer Programing Python - Midterm
3. What is the purpose of the "if __name__ == '__main__':" statement in a Python script?
a. It defines the main function of the program
b. It checks if the program is running as the main program or being imported as a
module
c. It imports the necessary modules for the program to run
d. It creates a new instance of the program class
1
4. Which of the following is the correct way to open a file in Python for reading?
a. file = open("filename.txt", "w")
b. file = open("filename.txt", "r")
c. file = open("filename.txt", "a")
d. file = open("filename.txt", "x")
a=2
b=3
c=a+b
print(c)
a. 5
b. 6
c. "5"
d. an error will occur
numbers = [1, 2, 3, 4, 5, 6]
target_sum = 7
print(f"There are {count_pairs(numbers, target_sum)} pairs of numbers in
{numbers} that add up to {target_sum}")
For example, if the input list is [1, 2, 3, 4] and the target sum is 5, the function should return 2,
since there are two pairs of numbers that add up to 5: (1, 4) and (2, 3)
2
1. Your program should include a class-based slot machine program that allows the
user to deposit an initial amount of money into their account.
2. The user should be able to select the number of lines they want to bet on
(between 1 and 3) and the amount of money they want to bet on each line
(between $1 and $100).
3. Constant variables to consider: MAX_LINES = 3, MAX_BET = 100, MIN_BET = 1,
ROWS = 3, COLS = 3, symbol_count = { "A": 2, "B": 4, "C": 6, "D": 8 }, symbol_value =
{ "A": 5, "B": 4, "C": 3, "D": 2 }.
4. The slot machine should generate a random selection of symbols for each
column.
5. The program should determine if the user has won any money based on the
symbols displayed and the user's bet.
a. Single line: If all three symbols on the line are the same, the user wins a
prize equal to the bet amount times the value of that symbol.
b. Two lines: If two of the three lines have matching symbols, the user wins a
prize equal to the bet amount times the value of the matching symbols.
c. Three lines: If all three lines have matching symbols, the user wins a prize
equal to the bet amount times the value of the matching symbols, plus a
bonus based on the count of the matching symbols (as defined in the
symbol_count dictionary).
6. If the user wins, their winnings should be added to their account balance. If they
lose, their bet should be subtracted from their account balance.
7. The user should be able to continue playing until they decide to quit or their
account balance is zero.
8. You are required to use classes to implement the Slot Machine program.
9. The class should have at least three methods: deposit (to deposit money into the
user's account), play (to play a round of the game), and play again (to ask the
user if they want to play again).
3
10. Use try and except instructions to control potential errors, such as when the user
inputs an invalid number of lines or bet amount.
"Don't let the fear of coding mistakes hold you back. Embrace the challenge, stay
focused, and code on!" Good luck