[Python] How can I fix the indentation so my program runs?

Discussão em 'Python' iniciado por Stack, Março 23, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to create a craps program but am struggling to get the indentation down for one of my while loops. The goal for the while loop #1 I am working with is to provide a message detailing how much the player either lost or won with the else statement prompting the player to press enter to continue rolling. Once loop #1 ends it should move onto the next loop that asks the user if they wish to continue playing or not, if yes then the craps game begins once more otherwise the loop and the program would end. Each time I try to run loop #1 I get an error regarding my indentation for the else statement in the loop. What can I do to resolve this issue? This is what I get when I run this in OnlineGDB:

    File "/home/main.py", line 51 else: TabError: inconsistent use of tabs and spaces in indentation

    Any advice is appreciated, code is located below

    ''' Craps Program '''

    import random
    money=100
    while money != 0:
    print ('Hey there! Wanna try your luck at craps? Despite the name it is quite addicting!')
    #start a game while loop
    #show the user how much money they have (give them some to start)
    print ('Looks like you got $',money,'to play with.')
    #ask if they want to bet
    #prompt the user to enter a bet and store it in the variable bet
    bet=money
    bet = int(input("How much will you bet?"))
    money-=bet
    if bet>money:
    print("You can't bet more than you have, but you can bet it all!")
    elif bet<=0:
    print("Yeah real funny there pal. Security!")
    exit()
    #if a player bets more money than they have or negative money, all money is placed

    #randint 1-6 twice, save each in their own variable die1, die2
    dice1 = random.randint(1,6)
    dice2 = random.randint(1,6)
    #add dice together and save as roll
    roll=dice1+dice2
    print("Looks like you got",dice1,"and",dice2,"which totals to",roll,)
    #if roll is 7 or 11 is rolled add bet to money, continue to restart
    if roll == 7 or roll == 11:
    money+=bet
    print("Hey you won! You're $",bet,"richer now! Wanna play again?")
    #elif 2, 3, or 12 subtract the money
    elif roll == 2 or roll == 3 or roll ==12:
    money-=bet
    print("Sorry, better luck next time. Say goodbye to those $",bet,", you wanna play again?")
    #else save that number as the point
    else:
    point = roll
    print("Now you need to roll a total of",point,"without getting a 7")
    while True:
    dice1 = random.randint(1,6)
    dice2 = random.randint(1,6)
    roll = dice1+dice2
    print("Looks like you got",dice1,"and",dice2,"which totals to",roll,)
    if roll == point:
    money+=bet
    print("Hey you won! You're $",bet,"richer now! Wanna play again?")
    break
    elif roll == 7:
    money-=bet
    print("Sorry, better luck next time. Say goodbye to those $",bet,", you wanna play again?")
    break
    else:
    input("It ain't over till it's over! Press Enter to keep rolling!")

    while True:
    user_input=input("Would you like to play again?")
    if user_input in ["yes", "y","Yes","Y"]:
    print("Excellent, let’s roll!")
    elif user_input in ["no", "n","No","N"]:
    print("Alright then, see ya!")
    break

    Continue reading...

Compartilhe esta Página