Python - 在 if 语句中找不到语法错误
Python - Can't find syntax error in if statement
我已经有一段时间没写代码了,所以我想重新开始,但我的一些代码遇到了问题。
我想写一个简单的程序,它接受用户的输入并检查它是否全是没有空格的字母,并且长度小于 12。我一直在网上收到 "Invalid syntax" 错误17 每当我 运行 代码,指向 if 语句后的冒号,检查用户名是否只是字母且少于 12 个字符。我知道这意味着在那之前的线路上有错误,但是在哪里?
#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!"
更新 - 我对代码做了更多的尝试,唯一改变的是 while
条件为 if
:
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
if check_valid(uinput):
print uinput
check_valid(uinput)
print "We hope you enjoy your stay here at Userspace!"
我运行这个代码,但得到这个错误:
File "Refresher In Python.py", line 39
return True
SyntaxError: 'return' outside function
抱歉我是个菜鸟。今天也刚加入 Stack Overflow。
我相信这就是你想要的。我建议将它分成两个函数,你的 check_valid() 和一个通用的 main() 函数。
def check_valid(usrnameinput):
if (not usrnameinput.isalpha()) or (len(usrnameinput) >= 12):
print("Invalid name")
return False
else:
print("Welcome!")
return True
def main():
uinput = raw_input("Enter a Username: ")
while not check_valid(uinput): #Repeatedly asks user for name.
uinput = raw_input("Enter a Username: ")
main()
我已经有一段时间没写代码了,所以我想重新开始,但我的一些代码遇到了问题。
我想写一个简单的程序,它接受用户的输入并检查它是否全是没有空格的字母,并且长度小于 12。我一直在网上收到 "Invalid syntax" 错误17 每当我 运行 代码,指向 if 语句后的冒号,检查用户名是否只是字母且少于 12 个字符。我知道这意味着在那之前的线路上有错误,但是在哪里?
#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!"
更新 - 我对代码做了更多的尝试,唯一改变的是 while
条件为 if
:
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
if check_valid(uinput):
print uinput
check_valid(uinput)
print "We hope you enjoy your stay here at Userspace!"
我运行这个代码,但得到这个错误:
File "Refresher In Python.py", line 39
return True
SyntaxError: 'return' outside function
抱歉我是个菜鸟。今天也刚加入 Stack Overflow。
我相信这就是你想要的。我建议将它分成两个函数,你的 check_valid() 和一个通用的 main() 函数。
def check_valid(usrnameinput):
if (not usrnameinput.isalpha()) or (len(usrnameinput) >= 12):
print("Invalid name")
return False
else:
print("Welcome!")
return True
def main():
uinput = raw_input("Enter a Username: ")
while not check_valid(uinput): #Repeatedly asks user for name.
uinput = raw_input("Enter a Username: ")
main()