The Student Room Group

coding help

not sure if I should post this here, but stack overflow was confusing
i have a project where i have to make a username check program (python)

username = input('Please enter your username:')
length = len(username)

if length < int('30'):
print('Username is accepted')

elif length >= int('30'):
print('Username rejected')

search = username.find('.')

elif search == '-1':
print('Username rejected')

i'm getting a syntax error and it is highlighting the line after the find() method and the function elif
i can't see what the syntax error is though
(edited 5 years ago)
Post your code as it is with [code ] [/code ] around it. This will keep the formatting. Remove the spaces though in the code brackets thing.

Or take a screenshot of the code and error
Reply 2
Original post by sunflower69
not sure if I should post this here, but stack overflow was confusing
i have a project where i have to make a username check program (python)

username = input('Please enter your username:')
length = len(username)

if length < int('30'):
print('Username is accepted')

elif length >= int('30'):
print('Username rejected')

search = username.find('.')

elif search == '-1':
print('Username rejected')

i'm getting a syntax error and it is highlighting the line after the find() method and the function elif
i can't see what the syntax error is though


Final elif option (after the find()) is outside the original if because of the search line
Move the
search =
line to before the first if.
Reply 3
no idea, just changed it
Original post by sunflower69

i'm getting a syntax error and it is highlighting the line after the find() method and the function elif
i can't see what the syntax error is though


Remember that 'elif' is a python keyword which is simply a short-hand form of 'else if'. 'if', 'elif' and 'else' all control the flow of your program, but they need to be joined together and happen in a specific order.

So any kind of 'else' clause must be related to an earlier 'if' clause because its purpose is to execute when the if condition is false. They all control the 'flow' of your program by deciding which bit of code to run, but if/elif/else must all be in the same "block" to work correctly. (Think about the word 'else' in plain English -- it means 'otherwise', or 'the other'; so in logical terms it's equivalent to "if the previous condition is false" -- which only makes sense if a previous condition exists, otherwise it's meaningless on its own)

Being in the same block means that indentation is extremely important; the if/elif/else statements must all flow between each other at the same level of indentation without anything interrupting them;

In other words, your if-elif statements need to join together like this - notice the indentation level of the if, elif or else statements and that there is nothing else at that same (or higher) level of indentation between them, so Python recognises that they're all part of the same flow-control block.


# if statement will run if something is true
if something:
doThing()

# elif statement will run if something is false but somethingElse is true
elif somethingElse:
doDifferentThing()

# else statement will run if something is false and somethingElse is false
else:
doOtherDifferentThing()


The only statements between them are at more-indented level, which is fine - Python recognises those as the code to run depending on whether each condition is true or not... There are no statements at the same indentation level or a less-indented level.

The following is disallowed in Python because there's a rogue instruction interrupting the if / elif block

# if statement will run if something is true
if something:
doThing()

# This is bad because it separates the the 'if' and 'elif'
callTheInterruptingSheep()

# This will throw an error because 'elif' is not in the same block as 'if'
elif somethingElse:
doDifferentThing()
(edited 5 years ago)
TRY THIS:username = input('Please enter your username:') length = len(username) search = username.find('.') print('search',search)if search == -1: print('Username rejected')elif length >= int('30'): print('Username rejected') else: print('Username is accepted')

Quick Reply

Latest

Trending

Trending