-2

I have to create a slot machine sort of in Python that displays numbers 0 - 5 and if the player gets a specific sequence of numbers they win a specific amount of money. I have created all the code to score the player and print it but I can't loop that block.

I want to start the loop from (if wants_play == "p" and player_score >= 0).

Here is my code:

import random

player_score = 1
round_score = 0

wants_play = input("Press 'P' to play or 'Q' to quit. ")
if wants_play == "p" and player_score >= 0:
        print("you have " + str(player_score) + " pound")
        num1 = random.randint(0, 5)
        num2 = random.randint(0, 5)
        num3 = random.randint(0, 5)
        print("you got numbers:", num1, num2, num3)

        round_score = 0
        player_score = 0.2
        if num1 == num2 or num1 == num3 or num2 == num3:
            round_score = 0.5
        if num1 == num2 and num1 == num3 and num2 == num3:
            round_score = 1
        if num1 == 0 and num2 == 0 and num3 == 0:
            round_score = 5
        if num1 == 1 and num2 == 1 or num1 == 1 and num3 ==1 or num2 == 1 and num3 == 1:
            round_score = -1
        if num1 == 1 and num2 == 1 and num3 == 1:
            round_score = 0
            player_score = 0

        print("you won ", round_score, " pounds!")
        player_score += round_score
        if player_score < 0:
            player_score = 0
else:
    print("Goodbye")
    exit()'''
M--
  • 25,431
  • 8
  • 61
  • 93
Arjun
  • 1
  • 2

1 Answers1

1

You cannot loop because you used an If instruction instead of a loop (while/for) An if instruction is executed just once, if the condition is fulfilled, otherwise, it goes on the else branch, also once. if you wanna loop, you should consider using a while instruction or a for instruction:

  • a while instruction will execute everything underneath it as long as the condition of the while is fulfilled (is true).

    While( wants_play == "p" and player_score >= 0): print "hello"

this will print hello as long as wants_play == "p" and player_score >= 0. it will not stop to print hello until one of them is changed or a break instruction is called.

While( wants_play == "p" and player_score >= 0): 
    print "hello"
    break

will only print hello once because the break keyword is called and this will terminate the execution of the loop

While( wants_play == "p" and player_score >= 0): 
    print "hello"
    player_score-=1

will print hello as long as the player_score >= 0; each time the loop is executed, 1 is substracted from player_score and it will eventually become <0, that is when the execution of the while will end

because you used the and keyword , both condition have to be fulfilled. if one of them is no longer true, the execution of the while will stop

 wants_play == "p" and player_score >= 0

I didnt really understand what you wanted to do with the player score and round score, how you wanted to implement them, but i modified the answer so it asks you each time if you want to continue or not

import random

player_score = 1
round_score = 0

wants_play = input("Press 'P' to play or 'Q' to quit. ")
while(wants_play=="P" or wants_play == "p"):

        print("you have " + str(player_score) + " pound")
        num1 = random.randint(0, 5)
        num2 = random.randint(0, 5)
        num3 = random.randint(0, 5)
        print("you got numbers:", num1, num2, num3)

        round_score = 0
        player_score = 0.2
        if num1 == num2 or num1 == num3 or num2 == num3:
            round_score = 0.5
        if num1 == num2 and num1 == num3 and num2 == num3:
            round_score = 1
        if num1 == 0 and num2 == 0 and num3 == 0:
            round_score = 5
        if num1 == 1 and num2 == 1 or num1 == 1 and num3 ==1 or num2 == 1 and num3 == 1:
            round_score = -1
        if num1 == 1 and num2 == 1 and num3 == 1:
            round_score = 0
            player_score = 0

        print("you won ", round_score, " pounds!")
        player_score += round_score
        if player_score < 0:
            player_score = 0

        wants_play = input("Press 'P' to play or 'Q' to quit. ")


print("Goodbye")
exit()