samedi 11 juin 2016

Python - Can't find syntax error in if statement

Its been a while since I've written code, so I'm trying to get back into it again, but I'm having trouble with some of my code.

I wanted to write a simple program that takes a user's input and checks to see if it is all letters with no spaces, and has a length less than 12. I keep getting an "Invalid syntax" error on line 17 whenever I run the code, pointing to the colon after the if statement that checks if the username is just letters and less than 12 characters. I know that means there's an error on the line before that, but where?

#import the os module

import os

#Declare Message

print "Welcome to Userspace - Your One-Stop Destination to Greatness!" + "\n" + "Please enter your username below." \
 + "\n" + "\n" + "Username must be at least 12 characters long, with no spaces or symbols." + "\n" + "\n"

#uinput stands for user's input

uinput = raw_input("Enter a Username: ")

#check_valid checks to see if arguement meets requirements

def check_valid(usrnameinput):
    if (usrnameinput != usrnameinput.isalpha()) or (len(usrnameinput) >= 12):
        os.system('cls')
        print "Invalid Username"
        return False
    else:
        os.system('cls')
        print "Welcome, %s!" % (usrnameinput)
        return True

#Asks for username and checks if its valid

print uinput
check_valid(uinput)

#Checks input to check_valid is correct, and if it is not, then ask for new username input and checks it again

while check_valid(uinput):
    return True
    break
else:
    print uinput
    check_valid(uinput)

print "We hope you enjoy your stay here at Userspace!"

Aucun commentaire:

Enregistrer un commentaire