2016-07-25 58 views
-3

我相对较新的python,在我的基础一年,我学到了BBC BASIC,这很基础,我在那里得到了很多坏习惯。 我在codecademy的帮助下学习了python,但是,如何在if语句中调用函数?在我的第一条if语句中,我调用了函数mainMenu(menu),但是它没有显示函数内容。为什么?如何在Python中的if语句中调用某个函数?

(通过我只是试图做一个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 "------------------------------------------------" 
+1

'mainMenu(menu)'那么,你没有在任何地方定义菜单,所以这将会崩溃。除此之外,不应该有任何问题。 –

+0

@MorganThrapp他必须将功能移到顶部,否则它不会被检测到。 –

回答

3

尝试把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() 
+1

@AdamSmith修正 – geek1011

1

由于在C++中,该功能必须在使用它的代码区之前定义因此,你的代码应该是存在的d:

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循环以上的功能,但要确保你的功能就是它被称为区域上方。

+1

正如MorganThrapp在评论中指出的那样,由于没有定义'menu',所以这会由于NameError而崩溃。 –

+0

是的,但我认为那只是他忘了复制的代码... –

4
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

谢谢!每天学习。 –