-1

I am learning python right now, and I'm working on making graphics. It was working for a while, making a window and displaying the correct graphics, but somehow I messed up the formatting and now it always shows an error when I run the program.

I have already tried removing the formatting and letting VS Code auto-format it for me, but it still shows the same error.

Here is first.py:

from graphics import *
import time
def isset(v):
    return v in locals() or v in globals()  
a = GraphWin('Test', 1000, 800)
a.setCoords(1000, -800, -1000, 800)
a.setBackground('white')
b = Entry(Point(0, 0), 20)
b.setFill('white')
b.draw(a);
c = Text(Point(0, 80), 'What is your name?')
c.draw(a)
while True:
    key = a.checkKey()
    if key == 'Return':
        if b.getText():
            d = Text(Point(0, 0), 'Hello, '+str(b.getText())+'!')
        b.setText('')
        b.undraw()
        c.undraw()
        d.draw(a)
        time.sleep(2)
        d.undraw()
        b.draw(a)
        c = Text(Point(0, 80), 'How old are you?')
        c.draw(a)
        break

while True:
    key = a.checkKey()
    if key == 'Return':
        if b.getText():
            d = Text(Point(0, 0), 'You are '+str(b.getText())+' years old!')
            b.undraw()
            c.undraw()
            d.draw(a)
            time.sleep(1)
        for i in range(5, 0, -1):
            if isset('e'):
                e.undraw()
            e = Text(Point(0, -80), 'Window closing in: '+str(i))
            e.draw(a)
            time.sleep(1)
            break

Here is the terminal output:

$ python3 first.py
  File "first.py", line 17
    d = Text(Point(0, 0), 'Hello, '+str(b.getText())+'!')
                                                        ^
TabError: inconsistent use of tabs and spaces in indentation

Please tell me what I am doing wrong!

I cannot figure out for the life of me what is going on with my code and it's driving me insane.

ezra
  • 307
  • 3
  • 13
  • copy your python code from above, save it to a file, and try to run that. this should remove all tabs. – jdigital Oct 19 '19 at 23:48
  • 1
    `if` is not a loop. Also, the error means what it says. I assume you know what a space is, and what a tab is. You are not allowed to assume that a tab represents a specific number of spaces, or to switch the order of spaces and tabs in the indentation. Practically speaking, you should choose one and stick with it. Even more practically, [use four spaces per indent and do not use tabs](https://www.python.org/dev/peps/pep-0008/) - that's the community standard. – Karl Knechtel Oct 19 '19 at 23:49
  • which text editor are you using? I have had similar challenges with vscode but can be easily resolved by altering your preferences. – cinch Oct 19 '19 at 23:49

1 Answers1

1

You are using both spaces and tabs for indentation. Python accepts both, as long as you are consistent. Either use tabs or spaces, not both.

Pedro Rodrigues
  • 2,520
  • 2
  • 27
  • 26