无法在 else 语句中重复函数获取错误 'str' 对象不可调用

Unable to repeat function in else statement getting error 'str' object not callable

所以我查看了不同的帖子,其中人们发布了相同的错误代码,并尝试通过将一些变量设为全局变量来对我的代码进行一些不同的修改。

def SchoolYear():
 os.system('clear')
 print('Please enter Which Year you are in School. EX.) 11th or Senior')
 print('')
 global SchoolYear
 SchoolYear = input('Please enter the class: ')
 if SchoolYear in ('11th', '12th', '10th', '9th', 'Senior', 'senior', 'freshman', 'Freshman', 'sophomore', 'Sopomore', 'junior', 'Junior' ):
    print('Good Job')
 else:
    print("Invalid Entry Please Try again...")
    print('')
    SchoolYear()

我想要的代码是,如果它与我的 critia 不匹配,然后重复相同的功能,但我不断得到错误代码 'str' object not callable

怎么样

var = raw_input("Please enter something: ")
print "you entered", var

if var in ['11th', '12th', '10th', '9th', 'Senior', 'senior', 'freshman', 'Freshman', 'sophomore', 'Sopomore', 'junior', 'Junior']:
    print('Good Job')
 else:
    print("Invalid Entry Please Try again...")

您想将上述功能包含在 class 中吗?

程序混淆了 SchoolYear 变量名和 SchoolYear 函数名。更改函数或变量的名称,它将起作用:

def SchoolYearFunc():
    os.system('clear')
    print('Please enter Which Year you are in School. EX.) 11th or Senior')
    print('')
    global SchoolYear
    SchoolYear = input('Please enter the class: ')
    if SchoolYear in ('11th', '12th', '10th', '9th', 'Senior', 'senior', 'freshman', 'Freshman', 'sophomore', 'Sopomore', 'junior', 'Junior' ):
        print('Good Job')
    else:
        print("Invalid Entry Please Try again...")
        print('')
        SchoolYearFunc()