-1

When I try to run my code it says:

if again == "y":
                ^

TabError: inconsistent use of tabs and spaces in indentation

My code is:

import random

def game():
    wrong_guesses = [1,2,3,4,5]
    num = random.randint(0,100)
    guess = input("Guess a number bewtween 1 and 100, you have five tries ")

    def wrong_guess():
        wrong_guesses.remove()
        print("That is not the number, You have {} guesses left".format(len(wrong_guesses)))

    def right_guess():
        if guess == num:
            print("Congratulations! You guessed the number. You had {} guesses left".format(len(wrong_guesses)))
            play_again()

    def game_over():
        if len(wrong_guesses) == 0:
            break
            play_again()

    def play_again():
        again = input("Would you like to play again? [y/n}")

        if again == "y":
            game()   
        elif again == "n":
            break

game()
DYZ
  • 55,249
  • 10
  • 64
  • 93
LJPOOD
  • 5
  • 2
  • 3
    Simplest solution is: don't use tabs in python code. In your existing code, replace all tabs with 4 spaces. Configure your editor to create 4 spaces when you press tab. – zvone Jun 19 '18 at 23:05
  • You have another indentation-related issue: the last `if` statement is inside the function `play_again`. (Must be outside, according to the logic of the program.) – DYZ Jun 19 '18 at 23:08
  • Ok thank you. for your help, but not I have a different problem. It is giving me the same error message but now with the game() at the very bottom of the page. – LJPOOD Jun 19 '18 at 23:12
  • I figured it out. – LJPOOD Jun 19 '18 at 23:12

2 Answers2

1

This question has already been answered numerous times, e.g.: "inconsistent use of tabs and spaces in indentation"

So first, please seek out the answer before posting a new question here on StackOverflow.

However, to provide my version of the answer:

The TabError tells you exactly why your code will not run. There is an inconsistency in your use of tabs vs spaces (e.g. you are using both tabs and spaces). The solution is to choose tabs or spaces and switch to it exclusively.

While Python does not require you to use one specifically, the PEP8 style guide advises spaces. You should be able to configure your editor to convert tabs to spaces. PEP8: https://www.python.org/dev/peps/pep-0008/

Also, while syntactically-correct, you should probably change your elif clause in the play_again function to an else (or at least add an else clause after the elif); the reason I say this is you are currently not handling any input other than "y" and "n". Not to mention your code is calling game() recursively as opposed to running in an infinite loop, which is going to cause the program to crash at a certain point; for that I would advise refactoring the game to a while True: loop and then the break would break out of the gameloop--this is traditionally how gameloops work at a high-level.

Zach King
  • 798
  • 1
  • 8
  • 21
0

The error says it all.

TabError: inconsistent use of tabs and spaces in indentation

This means you must have intertwined spaces and tabs in your code. Try removing all the white space and using only spaces (4 to replace a tab).

liamhawkins
  • 1,301
  • 2
  • 12
  • 30