Well, I'm a programming n00b...

I'm taking an Intro to Comp Sci course and Python is the main topic. Right now we were asked to complete this assignment:

Goal: Apply function top down design to a Python slot machine game.
The slot machines have between 2 and 6 reels and each reel will display one of 10 digits (0, ..., 9). So each spin will return a number with n digits (as many as reels selected). For each spin, the user determines the number of reels and spin = random.randint(0,10**(r-1)).

First, the user is prompted to enter the number of reels, which will not change throughout the game. The user is also prompted to enter the number of tokens and each spin will cost the player 1 token. The game ends when the user opts not to continue or when the user runs out of tokens.

The user hits the jackpot if the weight is equal to the number of reels. The weight is defined as the largest number of the same digits in one number (Ex. 234458 has weight 2 because 4 appears twice). Based on the number of reels and the weight, the user is awarded tokens (given in project but can be arbitrary for now except when weight = 1, award = 0).

For each spin, the user is shown the generated number, the weight, the award, their balance of tokens and are asked if they would like to continue the game.

It has to be a top down design and we must define our own functions. I'm really confused about defining functions in general, especially when it comes to arguments and the return line. Also, a particular aspect of the program that I'm confused about is how to tally the weight. Someone suggested to make an empty list but I'm not sure how to read the spin digit by digit to keep tally of the digits in the empty list. Here is what I have so far(and I don't believe any of it is correct):

from random import randint

def ask(r, tokens):
Asks user for reel and token inputs
r = input ('How many reels? (2, 3, 4, 5, or 6): ')
tokens = input ('Give the number of tokens: ')
return (r, tokens)

def spin(r, tokens):
Calculates and displays spin
while respond == 'y':
spin = randint (0,10**((r)-1))
tokens -=1
print 'spin '+str('%04d' % spin)+' has weight _ with award _'
if tokens <= 0:
print ('ending balance: ' + tokens)
respond = raw_input('your current balance: ' + str(tokens) + ', conintue? (y/n)')

def weight(spin):
Notes each digit in a spin
Determines weight based on greatest amount of same number in each spin
L = [0,0,0,0,0,0,0,0,0,0]
m = '-> spin %0d' % (spin)
if weight == 1:
print m + ', has weight 1 with award 0'
elif weight == 2:
total += weight
respond = raw_input('your current balance: ' + str(total) + ', conintue? (y/n)')
return (total,spin)

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!