【问题标题】:python-nothing is happening when i run my functionpython-当我运行我的函数时什么都没有发生
【发布时间】:2017-09-01 20:23:08
【问题描述】:

当我运行代码时,我的 while 循环运行良好,然后程序就结束了,即使我有一个完整的其他函数想要运行。 在这段代码之前有一个 while 循环,它以 login = False 结尾,我也尝试用 break 替换它,同样的事情发生了。

  import csv
  import sys

username="Leeman"
password="treeroad"

login = True
login_u = input("Enter username ")
login_p = input("Enter password ")

while login == True:

if login_u + login_p != username + password:
        print("incorrect login")
        sys.exit()
elif login_u != username:
        print("incorrect login")
        sys.exit()
elif login_p != password:
        print ("incorrect login")
        sys.exit()
elif login_u + login_p == username + password:
        print("Welcome to the system")
        login = False    

def main_menu():

print("---------------------------------School Menu-----------------------------------")
option=input("""Options:
                    1-Enter new student details
                    2-Search for student by ID number
                    3-View student details
                    4-Reports
                    5-Logout
                    Where do you want to go, 1,2,3,4 or 5?
                    """)

if option == "1":
   details=input("Enter your new student's details in format:ID Number,Forename,Surname,Gender,Tutor Group,DOB(dd/mm/yyyy),Phone Number,School Email: ")
   appendfile=open('classinfo.csv ' , 'a')
   appendfile.write(details)
   appendfile.close
   main_menu()

elif option=='2':
    with open ('classinfo.csv' , 'r') as classinfoFile:
        idnumber = input("Input the ID number of the student you wish to view")
        classinfoReader = csv.reader(classinfoFile)
        for row in classinfoReader:
            for field in row:
                if field == idnumber:
                   print (row)
                   main_menu()

此代码的目的是您通过选择一个数字 (1-5) 来选择您想要对 csv 文件执行的操作,然后在您完成后返回学校菜单。但是整个功能根本没有运行。为什么?

【问题讨论】:

  • 请重新排列您的代码
  • 你想怎么排列?
  • 你的缩进看起来很混乱。
  • 欢迎来到 Stack Overflow!请查看我们的"How to Ask" 页面。正如该页面所说,我们很乐意为您提供帮助,但如果没有minimal, complete, and verifiable example,我们将无法做到这一点。
  • 另外,在某些时候,您必须实际调用您的函数才能运行。

标签: python function


【解决方案1】:
def do_one():
    print("You entered 1!")

def do_two():
    print("You entered 2!")

def main():
    while True:
        choice = int(input("Enter 1, 2, or 3 (to exit): "))
        if choice == 1:
            do_one()
        elif choice == 2:
            do_two()
        elif choice == 3:
            print("Goodbye!")
            break

main()

【讨论】:

  • 我已经把它放在程序中看看会发生什么,当我运行它时会发生同样的事情,就像我在它根本不运行之后输入'def'任何东西
  • 嗯...这是您应该如何构建程序的示例。 def 创建一个新函数但不运行它。代码在最后一行运行,它调用main()。在main() 内部,您有一个while 循环,它将永远重复(或至少直到您到达break)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-08
  • 1970-01-01
相关资源
最近更新 更多