Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
469 views

Computer Programing Python - Midterm

The document describes a Python midterm exam that contains multiple choice and coding questions related to Python programming concepts. The multiple choice questions cover valid Python data types, for loops, the purpose of the if __name__ == '__main__' statement, and file opening modes. The coding questions involve defining a function to count pairs in a list that add up to a target number, and creating a class-based slot machine game simulation. The slot machine game requirements specify using classes, random symbol generation, win determination logic, updating the user's balance, and continuing play until their balance is empty. Try/except blocks should handle input errors.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
469 views

Computer Programing Python - Midterm

The document describes a Python midterm exam that contains multiple choice and coding questions related to Python programming concepts. The multiple choice questions cover valid Python data types, for loops, the purpose of the if __name__ == '__main__' statement, and file opening modes. The coding questions involve defining a function to count pairs in a list that add up to a target number, and creating a class-based slot machine game simulation. The slot machine game requirements specify using classes, random symbol generation, win determination logic, updating the user's balance, and continuing play until their balance is empty. Try/except blocks should handle input errors.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Computing Programming

with Python MidTerm


Professor: Ronny Estrella
Date: 23/04/2023

Multiple Selection Questions


1. Which of the following is NOT a valid data type in Python?
a. string
b. integer
c. boolean
d. decimal
e. array

2. What is the output of the following code?

fruits = ["apple", "banana", "cherry"]


for x in range(len(fruits)):
print(fruits[x])

a. apple banana cherry


b. apple, banana, cherry
c. 0 1 2
d. an error will occur

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")

5. What is the output of the following code?

a=2
b=3
c=a+b
print(c)

a. 5
b. 6
c. "5"
d. an error will occur

Correct the following exercise

def count_pairs(lst, target):


pairs = 0
for i in range(len(lst)):
for j in range(i+1, len(lst)):
if lst[i] + lst[j] == target:
pairs += 1
return pairs

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

Python Exercise Roulette Game


In this exercise you need to be able create a class-based slot machine program that will
simulate a user playing a slot machine game. Here are the instructions for the exam:

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.

Your program should be well-commented and easy to understand.

"Don't let the fear of coding mistakes hold you back. Embrace the challenge, stay
focused, and code on!" Good luck

You might also like