主要功能不会运行

时间:2018-06-02 18:04:45

标签: python function

#Declare main module
def main():

    #declare variables
    age = 0
    weight = 0
    month = ""

    #introduction
    def introduction():
        print("Welcome to Guess the secrects! ")
        print("""I want you to guess the age, wieght, and birth month to
            uncover my secrects!""" )

    #ask user for input
    def get_age():
        age = input("Enter age. ")
        return age

    def get_weight():
        weight = input("Enter weight. ")
        return weight

    def get_month():
        month = input("Enter a birth month. ")
        return month

    def correct_answers():
        age = get_age
        weight = get_weight
        month = get_month

    # We will now determine if the user guess the correct answers
    #using if statements.
    def check_answers(age, weight, month):
        if age <= 25:
            print("Congratulations, the age is 25 or less. ")
        if weight >= 128:
            print("Congratulations, the weight is 128 or more. ")
        if month == "April":
            print("Congratulations, the birth month is April. ")
        else:
            print("Try again! ")

main()

您好,我的问题是我的主要功能无法运行,并且Shell中没有显示任何内容。 任何建议或指导将不胜感激。

1 个答案:

答案 0 :(得分:1)

您对main()函数中其他函数的意图表明您尝试在函数中定义函数。

建议:尝试将主函数转换为类,然后将函数声明为主类中的方法,例如如下:

class main():

    #introduction
    def __init__ (self):

        #declare variables
        self.age = 0
        self.weight = 0
        self.month = ""
        print("Welcome to Guess the secrects! ")
        print("""I want you to guess the age, wieght, and birth month to
            uncover my secrects!""" )

    #ask user for input
    def get_age(self):
        self.age = input("Enter age. ")
        return self.age

    def get_weight(self):
        self.weight = input("Enter weight. ")
        return self.weight

    def get_month(self):
        self.month = input("Enter a birth month. ")
        return self.month



a = main()
a.get_age()
a.get_weight()
a.get_month()