#๐Ÿ”’ Code not working as I followed from a tutorial , no bugs too

30 messages ยท Page 1 of 1 (latest)

storm gulch
#

I followed the exact code done by the channel for a slot machine, and it doesnt work once i tried it in the beginning...

The code:

MAX_LINES = 3
MAX_BET = 100
MIN_BET = 1

def deposit():
while True:
amount = input("How much 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("How many lines would you like 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:
bet = input("How much would you like to bet on each line? $")
if bet.isdigit():
bet = int(bet)
if MIN_BET <= bet <= MAX_BET:
break
else:
print(f"Amount must be from ${MIN_BET} to ${MAX_BET}.")
else:
print("Please enter a number.")
return 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 current balance is {balance}")
    else:
        break
print(f"You are betting ${bet} on {lines}. Total bet is ${total_bet}.")

main()

The problem is that the code is not responding to the while loop in the function of main (the def main() one)

north charmBOT
#

@storm gulch

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

glossy trail
#

this issue with that code is that it doesn't loop back to the betting process after a bet is placed, which might make it seem like the while loop in the main() function isn't working

#

loop acutally runs as expected until a vailid bet is placed that is within the users balance

#

after that the program ends since there is no further code to execute

storm gulch
glossy trail
#
def main():
    balance = deposit()
    while balance > 0:
        print(f"Your current balance is ${balance}")
        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 current balance is ${balance}.")
            else:
                break
        
        balance -= total_bet
        print(f"You are betting ${bet} on {lines} line(s). Total bet is ${total_bet}.")
        print(f"Remaining balance is ${balance}.")

        if balance <= 0:
            print("You have run out of money!")
            break

        play_again = input("Do you want to play again? (y/n): ").lower()
        if play_again != 'y':
            break

    print("Thank you for playing!")
storm gulch
#

the exact code

#

but, i haven't written the resst

#

i just started

#

the seeming to be error occured

glossy trail
#

ok no worry

storm gulch
#

i just made a silly mistake....
i didnt save the file, i thought it gets autosaved, but it doesnt

#

saved it, now it works as i expected

#

thank you for the help anyway

glossy trail
#

i am glad you figured it out!

storm gulch
#

how to close the post now?
will the bot do it or do i have to do it myself?

glossy trail
#

you can just lock this post like this

storm gulch
#

self lock?

#

okay

#

thank you

glossy trail
#

no you should

storm gulch
#

kinda new to posts, so...

brittle sleet
#

!close

#

that should be it

storm gulch
#

thank you

#

!close

north charmBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.