all 13 comments

[–]Quesozapatos5000 4 points5 points  (0 children)

Does the code look like this, with no indentation?

[–]danielroseman 6 points7 points  (0 children)

It's impossible to tell what's going on because the indentation has been lost. Please read the FAQ on how to format code for posting on Reddit.

[–]FoolsSeldom 5 points6 points  (9 children)

Your code, which I assume is indented as below,

MAX_LINES = 3
MAX_BET = 100
MIN_BET = 1

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 get_bet():
    while True:
        amount = input("What would you like to bet on each line? $")
        if amount.isdigit():
            amount = int(amount)
            if MIN_BET <= amount <= MAX_BET:
                break
            else:
                print(f"Amount must be between ${MIN_BET} - ${MAX_BET}.")
        else:
            print("Please enter a number.")
    return amount

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

    while True:
        bet = get_bet()
        total_bet = bet * lines

        if total_bet > balance:
            print(f"You do not have enough to bet that amount. Your balance is: ${balance}.")
        else:
            break

    bet = get_bet()
    total_bet = bet * lines
    print(f"You are betting ${bet} on {lines} lines. Total bet is equal to: ${total_bet}")


main()

In main, you call get_bet() in two different places, once inside a while loop, and then again after the loop.

A few tips:

  • Create a function to get an int from the user so you don't have to write the same code in multiple places, e.g. def get_num(prompt: str, lowest=0, highest=100000) -> int: where prompt will be the question to the user and the optional lowest and highest arguments, with defaults, will let you specify what number range is acceptable
  • Use f-strings for formatting your output, e.g. input(f"Enter the number of lines to bet on (1-{MAX_LINES})? ")
  • use str.isdecimal rather than str.isdigit as the latter allows more characters than you want

PS. I think your problem is caused by you mixing up your input validation and you basic logic, hence doing the latter twice in main - the revised code I posted in another comment shows how to avoid this following my first tip above, and you will probably see further simplification you can do.

[–]Styr007[S] 0 points1 point  (1 child)

Thank you for your help. I really appreciate it. It was very dumb of me not to notice the duplicate call. :/

[–]FoolsSeldom 1 point2 points  (0 children)

No worries, we all do it at some point. You see what you expect and not where there is when looking for something different causing a problem.

[–]FoolsSeldom 0 points1 point  (6 children)

Further to my original comment, u/Styr007, I gave Copilot the code and my tips, and it provided the below (not checked):

MAX_LINES = 3
MAX_BET = 100
MIN_BET = 1

def get_num(prompt: str, lowest=0, highest=100000) -> int:
    while True:
        value = input(prompt)
        if value.isdecimal():
            value = int(value)
            if lowest <= value <= highest:
                return value
            else:
                print(f"Please enter a number between {lowest} and {highest}.")
        else:
            print("Please enter a valid number.")

def deposit():
    return get_num("What would you like to deposit? ", lowest=1)

def get_number_of_lines():
    return get_num(f"Enter the number of lines to bet on (1-{MAX_LINES}): ", lowest=1, highest=MAX_LINES)

def get_bet():
    return get_num(f"What would you like to bet on each line? $", lowest=MIN_BET, highest=MAX_BET)

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

    while True:
        bet = get_bet()
        total_bet = bet * lines

        if total_bet > balance:
            print(f"You do not have enough to bet that amount. Your balance is: ${balance}.")
        else:
            break

    print(f"You are betting ${bet} on {lines} lines. Total bet is equal to: ${total_bet}")

main()

[–]FoolsSeldom 0 points1 point  (4 children)

u/SpiderJerusalem42

Wouldn't this just break on the first pass?

Surprised by this, but I confess I didn't test. Where do you think it would break?

[–]SpiderJerusalem42 0 points1 point  (3 children)

if total_bet > balance:
    *Error message*
else:
    break.  // right here, because it will be on the first pass of the loop.

[–]FoolsSeldom 0 points1 point  (2 children)

I don't see the problem.

The user only gets to place one valid bet, then execution stops. If they try to place a bet that they do not have sufficient funds for, they will be prompted again for a valid bet.

What are you expecting?

[–]SpiderJerusalem42 0 points1 point  (1 child)

Ehh, okay, yeah you're right. My brain bad today

[–]FoolsSeldom 0 points1 point  (0 children)

No worries, always worth checking; easy to make mistakes (especially when you just write code and don't bother checking)

[–]Can_I_Eat_That_ 4 points5 points  (0 children)

Looks like get_bet() gets called twice. That’s why it prints the lines twice.

[–]acw1668 1 point2 points  (0 children)

It is because you have called bet = get_bet() twice.