简体   繁体   中英

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

So i have looked at different posts where people posted about the same error code and tried some different modifications of my code by making some variables global.

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()

All i want the code to de is if it doesnt match my critia then to repeat the same function but i contiously get the error code 'str' object not callable

The program is confusing the SchoolYear variable name with the SchoolYear function name. Change the name of the function or the variable and it will work:

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()

how about

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...")

is there a reason you want to enclose the above functionality inside a class?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM