2016-07-25 101 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

謝謝!每天學習。 –