如何在if函数中成功包含定义

时间:2016-09-02 13:09:10

标签: python

如何将定义函数放入if语句中,以便在运行程序时自动激活定义。

person=int(input("select the number of  any option which you would like to execute:"))

if (person)==(1):
    print ("please write the value for each class ")
    main()

def main ():
    juvenile=(input(" number of juveniles: "))
    adult= (input(" number of adults:"))
    senile=(input("number of seniles:"))

当我运行它时总是给我一个错误。

Traceback (most recent call last):
  File "C:\Users\fenis\Desktop\TEST PAGE 4 GCSE CS CW.py", line 6, in <module>
    main()
NameError: name 'main' is not defined
>>> 

1 个答案:

答案 0 :(得分:1)

您无法在未定义main()的位置致电main()。将您的main()定义移到您尝试调用的地方之上。

def main():
    juvenile = input("number of juveniles:")
    adult = input("number of adults:")
    senile = input("number of seniles:")

person = int(input("select the number of any option which you would like to execute:"))

if person==1:
    print ("please write the value for each class ")
    main()