如何在python中调用if语句中的某个函数?

时间:2016-07-25 19:58:00

标签: python function

我是python的新手,在我的预科课程中,我学习了BBC BASIC,这非常基础,我在那里学到了许多坏习惯。 我在codecademy的帮助下学习了python,但是,如何在if语句中调用函数?在我的第一个if语句中,我调用了函数mainMenu(menu),但它没有显示函数内容。为什么?

(顺便说一句,我只是想做一台ATM机只是为了练习我学到的东西并巩固它

print "Hello ! Welcome to JD's bank"
print
print "Insert bank card and press any key to procede"
print
raw_input()

passcode = 1111

attempts = 0

while passcode == 1111:

    passcodeInsertion= raw_input("Please insert your 4-digit code: ")
    print""


    if passcodeInsertion == str(passcode):
        print "This is working" #testing-----
        print ""
        mainMenu(menu)


    elif attempts < 2:

        print "Sorry ! Wrong passcode"
        attempts += 1

        print "------------------------------------------------"
        print ""
        print"Try again !! This is your " + str(attempts) + " attempt"
        print
        print "------------------------------------------------"
        print   

    else:
        print""
        print "Your card is unfortunately now blocked" 
        exit()

def mainMenu(menu):

    print "------------------------------------------------"
    print "Select one of this options"
    print "1. Check Balance"
    print "2. Withdraw Money"
    print "3. Deposit Money "
    print "0. Exit "
    print "------------------------------------------------"

3 个答案:

答案 0 :(得分:5)

print "Hello ! Welcome to JD's bank"
print
print "Insert bank card and press any key to procede"
print
raw_input()

def mainMenu():

    print "------------------------------------------------"
    print "Select one of this options"
    print "1. Check Balance"
    print "2. Withdraw Money"
    print "3. Deposit Money "
    print "0. Exit "
    print "------------------------------------------------"

passcode = 1111

attempts = 0

while passcode == 1111:

    passcodeInsertion= raw_input("Please insert your 4-digit code: ")
    print""


    if passcodeInsertion == str(passcode):
        print "This is working" #testing-----
        print ""
        mainMenu()


    elif attempts < 2:

        print "Sorry ! Wrong passcode"
        attempts += 1

        print "------------------------------------------------"
        print ""
        print"Try again !! This is your " + str(attempts) + " attempt"
        print
        print "------------------------------------------------"
        print   

    else:
        print""
        print "Your card is unfortunately now blocked" 
        exit()

尝试以上方法。将mainMenu移到顶部,您不需要任何参数。

答案 1 :(得分:4)

尝试将MainMenu功能置于顶部。这是因为在Python中,函数定义必须在它们使用之前。此外,您从未定义menu,因此我们可以摆脱它。

def mainMenu():
    print "------------------------------------------------"
    print "Select one of this options"
    print "1. Check Balance"
    print "2. Withdraw Money"
    print "3. Deposit Money "
    print "0. Exit "
    print "------------------------------------------------"

print "Hello ! Welcome to JD's bank"
print
print "Insert bank card and press any key to procede"
print
raw_input()

passcode = 1111

attempts = 0

while passcode == 1111:

    passcodeInsertion= raw_input("Please insert your 4-digit code: ")
    print""


    if passcodeInsertion == str(passcode):
        print "This is working" #testing-----
        print ""
        mainMenu()


    elif attempts < 2:

        print "Sorry ! Wrong passcode"
        attempts += 1

        print "------------------------------------------------"
        print ""
        print"Try again !! This is your " + str(attempts) + " attempt"
        print
        print "------------------------------------------------"
        print   

    else:
        print""
        print "Your card is unfortunately now blocked" 
        exit()

答案 2 :(得分:2)

与在C ++中一样,必须在使用它的代码区域之前定义该函数。因此,您的代码应为:

def mainMenu():

    print "------------------------------------------------"
    print "Select one of this options"
    print "1. Check Balance"
    print "2. Withdraw Money"
    print "3. Deposit Money "
    print "0. Exit "
    print "------------------------------------------------"

print "Hello ! Welcome to JD's bank"
print
print "Insert bank card and press ENTER to proceed"
print
raw_input()

passcode = 1111
attempts = 0

while passcode == 1111:
        passcodeInsertion= raw_input("Please insert your 4-digit code: ")
    print
    if passcodeInsertion == str(passcode):
        print "This is working" #testing-----
        print ""
        mainMenu()  #removed menu as you have not defined it above
    elif attempts < 2:
        print "Sorry ! Wrong passcode"
        attempts += 1

        print "------------------------------------------------"
        print ""
        print"Try again !! This is your " + str(attempts) + " attempt"
        print
        print "------------------------------------------------"
        print   
    else:
        print""
        print "Your card is unfortunately now blocked" 
        exit()

在其他地方,您可以将功能放置在while循环的正上方,但要确保您的功能位于调用它的区域之上。