Indention issue in Python IDLE

2019-09-01 10:53发布

问题:

IDLE config is set as

indentation width - 4 spaces
key set - IDLE classic windows
at startup - Open shell window
paragraph reformat width (in characters) - 70

I open up python IDLE and I am in the shell window. I click file > new file. I am now in the text editor. I write a line of code. It doesnt matter what the syntax is. When i push the "enter" button on my keyboard. Instead of dropping down to the next line and being in the proper spot. The little thing that blinks to show you where your at on the page is near the middle of the page...

number = float(input('what is your number? ')
               name = input('what is your name? ')


name = input('what is your name?')
               this is where it indents to
               it doesnt matter the syntax

number = int(input('what is your number?')
               if number > 5
               print (""" I am only using the enter key to
go to the next line""")
               else:
               print('this is what it does')

even if double spaced, should it not look like this?

number = int(input('what is your number?')
    if number < 5555
        print (' helloe')
    else:
        print('..............')

回答1:

You need to make sure that you have your parenthesis matched. In your example you are missing one ')' at the first line.

number = float(input('what is your number? ') ) # this last parenthesis is missing in your example
name = input('what is your name? ')

The code would probably not run. You should make it a habit of always writing the closing parenthesis directly when you start an expression. In that way you know you've got them matched.

The reason why you get strange indentation is because without the closing parenthesis the editor thinks that the next line is part of the previous expression, which is your

number = float(input('what is your number? ')
               # notice how the indentation is right below
               # the opening parenthesis
               )
name = input('what is your name? '
             # notice how this indentation has a
             # a different position
             )


回答2:

You have mistake on:

               if number > 5

Correct is:

               if number > 5:

and you wrote:

               name = input('what is your name? ')

but you should write:

name = input('what is your name? ')

without all that spaces.