为什么一个函数被调用而另一个函数被重复?

时间:2015-05-07 13:18:12

标签: python python-3.x

我必须做一个洗衣公司软件的小组项目。 login_Driver()函数最后有一个if语句,如果为true,则调用另一个函数但不调用该函数。

def login_Driver():
    print("Welcome to Sky Laundry")
    print("Login Driver Menu")
    print("Enter Username")
    driver_username = str(input())
    print("Enter Password")
    driver_password = str(input())
    '''text_file = open("driver_username.txt", "w")
    text_file.write(driver_username)
    text_file.close()'''

    text_file = open("driver_username.txt", "r")
    driver_username_check = text_file.readline()
    text_file.close()

    text_file = open("driver_password.txt", "r")
    driver_password_check = text_file.readline()
    text_file.close()

    if driver_username_check == driver_username and driver_password_check == driver_password:
        print("Login successful")
        return driver_username
        driver_Account()
    else:
        print("Login failed")




def login_Customer():
    print("Welcome to Sky Laundry")
    print("Login Customer")
    print("Enter Username")
    customer_username = str(input())
    print("Enter Password")
    customer_password = str(input())
    '''text_file = open("customer_username.txt", "w")
    text_file.write(customer_username)
    text_file.close()'''

    text_file = open("customer_username.txt", "r")
    customer_username_check = text_file.readline()
    text_file.close()

    text_file = open("customer_password.txt", "r")
    customer_password_check = text_file.readline()
    text_file.close()

    if customer_username_check == customer_username and customer_password_check == customer_password:
        print("Login successful")
        customer_Account()
        return customer_username
    else:
        print("Login failed")



def create_Account():
    print("Welcome to Sky Laundry")
    print("Enter whom you want to register as?")
    print("1.) Driver")
    print("2.) Customer")
    choice = int(input())
    if choice == 1:
        print("Driver Registration Menu")
        print("Enter your Username")
        new_driver_username = str(input())
        print("Enter you Password")
        new_driver_password = str(input())

        text_file = open("driver_username.txt", "w")
        text_file.write(new_driver_username)
        text_file.close()

        text_file = open("driver_password.txt", "w")
        text_file.write(new_driver_password)
        text_file.close()

        print("Registration Successful, taking you back to the Main Menu")
        main_Selection()
    elif choice == 2:
        print("Customer Registration Menu")
        print("Enter your Username")
        new_customer_username = str(input())
        print("Enter you Password")
        new_customer_password = str(input())

        text_file = open("customer_username.txt", "w")
        text_file.write(new_customer_username)
        text_file.close()

        text_file = open("customer_password.txt", "w")
        text_file.write(new_customer_password)
        text_file.close()

        print("Registration Successful, taking you back to the Main Menu")
        main_Selection()
    else:
        print("INVALID OPTION")


def driver_Account():
    print("Drivers Account")

def customer_Account():
    print(customer_account_name = login_Customer())
    print("Account")




print("Welcome to Sky Laundry")
print("1.) Login Driver")
print("2.) Login Customer")
print("3.) Create Account")
print("4.) User Exit")


def main_Selection():
    print("Welcome to Sky Laundry")
    print("1.) Login Driver")
    print("2.) Login Customer")
    print("3.) Create Account")
    print("4.) User Exit")

    if opt is 1:
        login_Driver()
    elif opt is 2:
        login_Customer()
    elif opt is 3:
        create_Account()
    elif opt is 4:
        login_Driver()
    else:
        print("INVALID OPTION PLEASE TRY AGAIN")
        main_Selection()

opt = int(input())
main_Selection()

这是输出

>>> 
Welcome to Sky Laundry
1.) Login Driver
2.) Login Customer
3.) Create Account
4.) User Exit
1
Welcome to Sky Laundry
1.) Login Driver
2.) Login Customer
3.) Create Account
4.) User Exit
Welcome to Sky Laundry
Login Driver Menu
Enter Username
levsingh
Enter Password
12345
Login successful
>>> 

2 个答案:

答案 0 :(得分:2)

你有

    return driver_username
    driver_Account()

返回语句将始终结束您所处的功能并且不再执行任何行,因此您永远不会得到更多。您需要翻转这两行的顺序才能调用driver_Account。

同样在您的main_Selection中,您不会要求输入,因此用户永远不会提供新号码。无效的输入将导致无限递归并使程序崩溃。你还将函数的主体留在了自身之外。因此,在您调用该功能之前,所有打印行都将打印出来。

答案 1 :(得分:0)

你有一个 -

return driver_username

在致电driver_Account()之前。

您应该使用主函数 -

,而不是在函数定义之间插入代码
if __name__ == "__main__":
    print("Welcome to Sky Laundry")
    print("1.) Login Driver")
    print("2.) Login Customer")
    print("3.) Create Account") 
    print("4.) User Exit")
    opt = int(input())
    main_Selection()