【问题标题】:Python - Can't find syntax error in if statement [closed]Python - 在if语句中找不到语法错误[关闭]
【发布时间】:2016-10-12 13:35:26
【问题描述】:

我已经有一段时间没有编写代码了,所以我试图重新开始编写代码,但是我的一些代码遇到了问题。

我想编写一个简单的程序,它接受用户的输入并检查它是否全是没有空格的字母,并且长度小于 12。每当我在第 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!"

更新 - 我更多地使用了代码,我唯一改变的是 whileif 为条件:

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。

【问题讨论】:

  • 愚蠢的问题:哪一个第17行?逐字添加完整的错误! (顺便说一句,这是显而易见的事情)
  • 您在函数之外有一个return 语句。你希望return Truewhile check_valid(uinput): 块中做什么? return 仅在 在函数中 有意义,而您的 while 循环不在函数中。
  • 感谢您的所有回复!所有这些都非常有帮助。另外,@MarcusMüller,第 17 行是 if (usrnameinput != usrnameinput.isalpha()) or (len(usrnameinput) >= 12):
  • @Bearclaw 不要把它放在评论中,把它放在实际问题中。并添加完整的错误。
  • 这里发布的代码没有语法错误,除了我提到的return的使用。所有check_valid 函数都可以正常解析。

标签: python if-statement syntax-error


【解决方案1】:

我相信这就是你想要的。我建议把它分成两个函数,你的 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()

【讨论】:

  • 当我尝试按照您的建议运行它时出现此错误:File "Refresher In Python.py", line 39 return True SyntaxError: 'return' outside function 这是一个空行。
  • 我相信你还有一些其他的代码会干扰这个。如果您完全按照我发布的内容运行,那不应该发生。我刚刚发布的代码中没有“返回”外部函数。
  • 我认为我的 IDE 出了点问题,在我重新启动程序之前无法正确运行代码。成功了,谢谢。
  • 如果我的代码有帮助,如果您接受答案,我将不胜感激。否则,有一个伟大的:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-25
  • 1970-01-01
  • 2012-12-20
相关资源
最近更新 更多