2015-09-11 36 views
-2

我寫了一個多選題程序,它不像我期望的那樣行事。Python - if else - 程序流程

我希望它這樣的行爲 - 當您鍵入1,去下面#USD塊和

  1. 當你鍵入2,它應該去塊下面#Euro

這裏是我的代碼:

print "Welcome to Currency Converter By Fend Artz" 
print "your options are:" 
print " " 
print "1) GDP -> USD" 
print "2) GDP -> Euro" 
USD = int(raw_input('')) 
if USD == 1: 
    choice = USDchoice  
elif USD == 2: 
    choice = EUROchoice 
else: 
    print ("You have to put a number 1 or 2") 
    int(raw_input('')) 
#USD 
def USDchoice(): 
    userUSD = float(input('How many pounds do you want to convert?(e.g. 5)\n')) 
    USD = userUSD * 0.65 
    print userUSD, "Pounds =",USD,"USDs" 

#Euro 
def EUROchoice(): 
    userEURO = float(input('How many pounds do you want to convert?(e.g. 5)\n')) 
    Euro = userEURO * 1.37 
    print userEURO, "Pounds =",Euro,"Euros" 

#Thing so the script doesn't instantly close 
Enter = raw_input('press ENTER to close\n') 
+5

選擇= USDchoice() – Alexander

+0

要設置'choice'的功能,但你不叫它。 – cmd

+0

添加()使其調用該函數。 –

回答

1

代碼有兩個錯誤。

  1. 您設置變量choice是你的你的功能之一的引用:USDChoiceEUROChoice。你需要調用這些函數,使用括號將變量設置爲它們返回的值。正如幾位評論指出的那樣,您可以像USDChoice()EUROChoice()這樣做。
  2. 您嘗試在創建它們之前調用這些函數。他們需要在上面移動,因爲所有東西都在全局範圍內(模塊級)。

固定碼:

#USD 
def USDchoice(): 
    userUSD = float(input('How many pounds do you want to convert?(e.g. 5)\n')) 
    USD = userUSD * 0.65 
    print userUSD, "Pounds =",USD,"USDs" 


#Euro 
def EUROchoice(): 
    userEURO = float(input('How many pounds do you want to convert?(e.g. 5)\n')) 
    Euro = userEURO * 1.37 
    print userEURO, "Pounds =",Euro,"Euros" 


print "Welcome to Currency Converter By Fend Artz" 
print "your options are:" 
print " " 
print "1) GDP -> USD" 
print "2) GDP -> Euro" 
USD = int(raw_input('')) 

if USD == 1: 
    choice = USDchoice() 
elif USD == 2: 
    choice = EUROchoice() 
else: 
    print ("You have to put a number 1 or 2") 
    int(raw_input('')) 

#Thing so the script doesn't instantly close 
Enter = raw_input('press ENTER to close\n') 
+0

非常感謝THOOO非常有幫助:D –