0

I can not get this python slot machine to function. Also, how do add a counter to track and display the number of times the user plays, and an accumulator to track and display the total amount of money won. The outer loop with a sentinel value which prompts whether the user wishes to continue playing is not working.

import random

words = ["Cherries", "Oranges", "Plums","Melons", "Bells"]


rand1 = random.randint(0,4)
rand2 = random.randint(0,4)
rand3 = random.randint(0,4)


word1 = words[rand1]
word2 = words[rand2]
word3 = words[rand3]

def random_calculate (calculate):

    print(word1, " ", word2, " ", word3)
    if (rand1 == rand2 or rand2 == rand3 or rand1 == rand3):
        if (rand1 == rand2 and rand2 == rand3):
            if(ranrand1=="Bells"):
                win=50           

        else:
            win = 10

    else:
         win = 5










def display_menue():
    print ("Welcome to the Slot Machine!")
    print ("    Match 2 of any kind to win $5")
    print ("    Match 3 of any fruit to win $10")
    print ("    Match 3 BELLS to win $50!")



def main():
    display_menue()
    calculate = random_calculate 

    choice = "y"
    while choice.lower() == "y":


        choice = input("Do you want to play? (y or n):  ")
        print("OK, pull the lever!")
        print()
        print("You have won $" + str(win))   





if __name__ == "__main__":
    main()
gneis
  • 31
  • 4
  • You need to make a [mcve] – wjandrea Jun 10 '19 at 20:11
  • A couple issues I see: Your `random_calculate` doesn't `return` anything, and `win` isn't defined elsewhere in the function. Also, that function takes in `calculate` as an argument, but doesn't use it anywhere, and in your line `calculate = random_calculate`, the function isn't called correctly (no parentheses or argument given) – G. Anderson Jun 10 '19 at 20:20

2 Answers2

0

Python 2.7 doesn't do well with type checks. So when you compared an integer with string, it didn't say anything :) Try this out, and let's get that slot machine working:

print(word1, " ", word2, " ", word3)
    if rand1 == rand2 == rand3:
        if rand1 == 4:
            win = 50
        else:
            win = 10
    elif rand1 == rand2 or rand2 == rand3 or rand1 == rand3:
        win = 5
    else:
         win = 0

In order to add a Counter and accumulator, you can add them as external variables.

counter = 0 total_winnings = 0

In the random_calculate function, you can add these 2 lines at the end:

count += 1 total_winnings += win

  • `word1` is being set to the string from `words` with index `rand1` and compared to other strings, so I don;t see the issue here unless I'm missing something – G. Anderson Jun 10 '19 at 20:20
0

First of all, sorry if my answer is poorly formatted, this is my first time answering. Second of all, I'll provide you the code I corrected and some details.

So, I assume, that your random_calculate() method is called so that it will regenerate the slots. To achieve this, you have to include the number generation into that method, so that it will regenerate each time it is called. Also in this function, since you don't pass any variables into the method, you should delete that "calculate" from there. Also, why store the random generated numbers, when you could call them once, and then compare the strings to each other, thus using less variables? Also, if the variable "win" was supposed to be returned to the main() function, you have to return that variable if it isn't declared as a global variable, which you shouldn't do. I also have changed the way you print out strings, because I personally prefer .format() over concatenation, but I might not be the only one.

Here's the modified code for the random_calculate() method:

def random_calculate():
    word1 = words[random.randint(0, 4)]
    word2 = words[random.randint(0, 4)]
    word3 = words[random.randint(0, 4)]

    print("{} {} {}".format(word1, word2, word3))
    if word1 == word2 or word2 == word3 or word1 == word3:
        if word1 == word2 and word2 == word3:
            if word1 == "Bells":
                win = 50
        else:
            win = 10
    else:
        win = 5

    return win

As for the main() function, I've added two counters: one for the number of iterations (num_of_plays) and a variable that tracks all of the gains throughout the game (sum_of_wins). Also, I have moved the variable win that gets it's value from random_calculate() into the core of the loop.

Here's the modified code for the main() method:

def main():
    display_menue()

    choice = "y"
    num_of_plays = 1
    sum_of_wins = 0
    while choice.lower() == "y":
        choice = input("Do you want to play? (y or n):  ")
        print("Try no. {}".format(num_of_plays))
        win = random_calculate()
        print("OK, pull the lever!")
        print()
        print("You have won ${}".format(win))
        sum_of_wins += win
        print("Your earnings through this playthrough: ${}".format(sum_of_wins))
        num_of_plays += 1

I hope this will help you!

trezvo
  • 1
  • 2