简体   繁体   English

我怎样才能让我的主程序首先出现,我怎样才能在我的主程序中调用我的函数?

[英]How can I make my main program appear first and how can I call my functions in my main program?

Right now I've tried making my main program the main "function" in my code.现在我已经尝试让我的主程序成为我代码中的主要“功能”。 But when I run it, the analysis bit comes up first which is not what i want.但是当我运行它时,分析位首先出现,这不是我想要的。 I want to make my code to do the following: output the main program so the user can input the marks total and input the marks scored in a test.我想让我的代码执行以下操作:输出主程序,以便用户可以输入总分并输入测试中的分数。

"Write a program that inputs a mark from the keyboard for sections of a project: 'analysis', 'design', 'implementation' and 'evaluation'. The program should output the total mark, the grade, and how many more marks were needed to get into the next mark band." “编写一个程序,从键盘输入项目部分的分数:'分析'、'设计'、'实施'和'评估'。该程序应该输出总分、成绩以及还有多少分需要进入下一个标记带。”

That was my task.那是我的任务。


    def analysis():
        print("Welcome to the analysis section")
        marktotal=int(input("Input mark total which is out of /100"))
        marks=int(input("Input marks"))
        totalanalysis=print("You got",marks,"/",marktotal)
        if marks > 80 or marks == 80:
            print("A*")
        elif marks > 67 or marks == 67:
            print("A")
        elif marks > 54 or marks == 54:
            print("B")
        elif marks > 41 or marks == 41:
            print("C")
        elif marks > 31 or marks == 31:
            print("D")
        elif marks > 22 or marks == 22:
            print("E")
        elif marks > 13 or marks == 13:
            print("F")
        elif marks > 4 or marks == 4:
            print("G")
        elif marks ==0:
            print("U")
        return
    analysis()

    def design():
        print("Welcome to the design section")
        marktotal=int(input("Input mark total which is out of /100"))
        marks=int(input("Input marks"))
        totalanalysis=print("You got",marks,"/",marktotal)
        return

    design()


    def main():
        selectedsection=str(input("What section would you like to grade?"))#main program starts here
        if selectedsection =="analysis":
            analysis()
        elif selectedsection =="design":
            print("design")
        elif selectedsection =="implementation":
             print("implementation")
        elif selectedsection =="evaluation":
             print("evaluation")

    if __name__ == "__main__":
        main()

不要调用函数,从 main 函数之外的代码中删除analysis()design()

I have taken your code and made some changes so that it is working.我已经使用了您的代码并进行了一些更改以使其正常工作。 I hope this helps you to find your problem.我希望这可以帮助您找到您的问题。

The part around your你周围的部分

if __name__ == "__main__":

looks strange.看起来很奇怪。 The main() function should be on its own line. main() 函数应该在它自己的行上。 Your posted code also had some indentation problems and the closing backticks should not be shown at the end.您发布的代码也有一些缩进问题,最后不应显示结束反引号。 I cannot change your post, because I have not enough reputation for that.我不能改变你的帖子,因为我没有足够的声誉。 The occurence of the function calls "analysis(), design() and main()" seem weird.函数调用“analysis()、design()和main()”的出现似乎很奇怪。 I have deleted analysis() and design().我已经删除了 analysis() 和 design()。 Also I have moved main() to its own line.我也将 main() 移到了自己的行。 Hard to say, if your original code is exactly the same as the posted code.很难说,如果您的原始代码与发布的代码完全相同。

def analysis():
    print("Welcome to the analysis section")
    marktotal=int(input("Input mark total which is out of /100"))
    marks=int(input("Input marks"))
    totalanalysis=print("You got",marks,"/",marktotal)
    if marks > 80 or marks == 80:
        print("A*")
    elif marks > 67 or marks == 67:
        print("A")
    elif marks > 54 or marks == 54:
        print("B")
    elif marks > 41 or marks == 41:
        print("C")
    elif marks > 31 or marks == 31:
        print("D")
    elif marks > 22 or marks == 22:
        print("E")
    elif marks > 13 or marks == 13:
        print("F")
    elif marks > 4 or marks == 4:
        print("G")
    elif marks ==0:
        print("U")
    return

def design():
    print("Welcome to the design section")
    marktotal=int(input("Input mark total which is out of /100"))
    marks=int(input("Input marks"))
    totalanalysis=print("You got",marks,"/",marktotal)
    return

def main():
    selectedsection=str(input("What section would you like to grade?"))#main program starts here
    if selectedsection =="analysis":
        analysis()
    elif selectedsection =="design":
        print("design")
    elif selectedsection =="implementation":
        print("implementation")
    elif selectedsection =="evaluation":
        print("evaluation")

if __name__ == "__main__":
    main()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将我的主程序与 GUI“合并”? - How can I 'merge' my main program with a GUI? 如何在我的python程序中显示以下文本? - How can I make the following text appear in my python program? 为什么内置函数在 python 中没有被归类为可调用的,我怎样才能让它们在我的程序中显示为可调用的? - Why aren't builtin functions classified as callable in python and how can I make them appear callable in my program? 如何在与主程序不同的线程中编写套接字服务器(使用gevent)? - How can I write a socket server in a different thread from my main program (using gevent)? 如果主程序使用特定变量,如何在我的模块中计算? - How can I figure out in my module if the main program uses a specific variable? 如何修复此网格板以不与程序中的主要细节重叠? - How can i fix this grid board to not to be overlapping the main details in my program? 如何从我制作的存储在主程序中的全局变量中的函数中获取字符串? - How can I get a string from a function I made to be stored in a global variable I have in my main program? 如何从我的 python 程序中调用智能合约? - how can I call smart contract from my python program? 如何使我的程序回到Python的开头? - How can I make my program return to the beginning in Python? 如何让我的 python 程序在后台运行 - How can i make my python program work in background
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM