Slot Game in Py

35 views Asked by At

I am building a very basic slot machine in python. This is the first python code I am doing, and am following a ytb tutorial. Somehow I am not getting the right result in terminal when I am testing out the code.

MAX_LINES = 3

def deposit():
    while True:
        amount = input("What would you like to deposit? $")
        if amount.isdigit():
            amount = int(amount)
            if amount > 0:
                break
            else:
                print("Amount must be greater than 0.")
        else:
                    print("Please enter a number.")

                    return amount



def get_number_of_lines():
      while True:
        lines = input("Enter the number of lines to bet on (1-" + str(MAX_LINES) + ")? ")
        if lines.isdigit():
            lines = int(lines)
            if 1 <= lines <= MAX_LINES:
                break
            else:
                print("Enter a valid number of lines.")
        else:
                    print("Please enter a number.")

                    return lines


def main():
    balance = deposit()
    lines = get_number_of_lines()
    print(balance, lines)

main()

This is the code I wrote so far.

And here is what I get in Terminal ( I am doing this in VsCode).

What would you like to deposit? $200

Enter the number of lines to bet on (1-3)? 3

None None. \<\<this should say 3 ... 

and further I did not get yet, but its a struggle to find the answer to this problem, there were some other people under the YouTube video who struggle/d with the same output in terminal.

photo of my screen when I run debug

3

There are 3 answers

1
quamrana On

You need to indent your return ... lines correctly:

MAX_LINES = 3

def deposit():
    while True:
        amount = input("What would you like to deposit? $")
        if amount.isdigit():
            amount = int(amount)
            if amount > 0:
                break
            else:
                print("Amount must be greater than 0.")
        else:
                    print("Please enter a number.")

    return amount

def get_number_of_lines():
    while True:
        lines = input("Enter the number of lines to bet on (1-" + str(MAX_LINES) + ")? ")
        if lines.isdigit():
            lines = int(lines)
            if 1 <= lines <= MAX_LINES:
                break
            else:
                print("Enter a valid number of lines.")
        else:
                    print("Please enter a number.")

    return lines

def main():
    balance = deposit()
    lines = get_number_of_lines()
    print(balance, lines)

main()

Sorry, yes the while True: in def get_number_of_lines(): was also indented incorrectly.

0
Patrick Abraham On

You need to only return after the while loop has finished. Also please try to maintain 4 spaces or one tab as the default indentation level.

MAX_LINES = 3

def deposit():
    while True:
        amount = input("What would you like to deposit? $")
        if amount.isdigit():
            amount = int(amount)
            if amount > 0:
                break
            else:
                print("Amount must be greater than 0.")
        else:
                    print("Please enter a number.")

    return amount



def get_number_of_lines():
    while True:
        lines = input("Enter the number of lines to bet on (1-" + str(MAX_LINES) + ")? ")
        if lines.isdigit():
            lines = int(lines)
            if 1 <= lines <= MAX_LINES:
                break
            else:
                print("Enter a valid number of lines.")
        else:
                    print("Please enter a number.")

    return lines


def main():
    balance = deposit()
    lines = get_number_of_lines()
    print(balance, lines)

main()
0
OldBoy On

The return statements of both your functions are indented too far. So when the code reaches the break statement, the function returns but does not pass any value back to the caller, so the captured variable is set to None. They should both be at the same level of indentation as the controlling while, so they actually return a value when the break clause is initiated.

def deposit():
    while True:
        amount = input("What would you like to deposit? $")
        if amount.isdigit():
            amount = int(amount)
            if amount > 0:
                break
            else:
                print("Amount must be greater than 0.")
        else:
                    print("Please enter a number.")

    return amount



def get_number_of_lines():
    while True:
        lines = input("Enter the number of lines to bet on (1-" + str(MAX_LINES) + ")? ")
        if lines.isdigit():
            lines = int(lines)
            if 1 <= lines <= MAX_LINES:
                break
            else:
                print("Enter a valid number of lines.")
        else:
                    print("Please enter a number.")

    return lines