2014-10-31 40 views
-3

我需要編寫一個get_input函數,它具有循環驗證,因此編號不能小於0 這是程序。我做了這個全球功能,但我的老師告訴我這是錯誤的。製作get_input一個全球性的功能似乎工作,但我需要使用需要幫助在python中定義一個函數

def get_input(): 

get_input() 

香港專業教育學院的手段一直是全球功能設置爲get_input =輸入,只是因爲我不知道怎麼做我上面貼無歌廳錯誤「全局名稱未定義」。

任何幫助,將不勝感激

get_input = input 

def main(): 
    pennies = get_input("Enter pennies : ") 
    nickels = get_input("Enter nickels : ") 
    dimes = get_input("Enter dimes : ") 
    quarters = get_input("Enter quarters : ") 

    print("You entered : ") 
    print("\tPennies : " , pennies) 
    print("\tNickels : " , nickels) 
    print("\tDimes : " , dimes) 
    print("\tQuarters : " , quarters) 


    total_value = get_total(pennies, nickels, dimes, quarters) 
    dollars = get_dollars(pennies, nickels, dimes, quarters) 
    left_over_cents = get_left_over_cents(pennies, nickels, dimes, quarters) 

    print("Total = $", total_value, sep="") 
    print("You have", dollars, "dollars and", left_over_cents, "cent(s)") 

main() 
+0

請出示您的嘗試之一以上,我們將能夠幫助你。 – 2014-10-31 17:47:19

+0

那麼,哪裏*是你的'get_input'的定義?您發佈的內容中沒有語法錯誤。您可能會發現[this](http://stackoverflow.com/q/23294658/3001761)有用。 – jonrsharpe 2014-10-31 17:47:22

+0

你已經添加了更多的解釋,但它仍然不清楚你定義'get_input'的位置。如果你可以發佈整個程序,包括'get_input'的定義,會更容易。 – Stuart 2014-10-31 17:55:50

回答

1

看起來你只是需要把你的raw_input所有的報表功能get_input裏面。

def get_input(currency): 
    currency = -1.0  
    while currency < 0: 
     try: 
      currency = float(raw_input("Enter %s: ", % (currency))) 
     except ValueError: 
      print "Invalid input!" 
      currency = -1.0 
      continue 
     if currency < 0: 
      print "Can't have negative money!" 
     else: 
      return currency 

def main(): 
    pennies = get_input("pennies") 
    nickles= get_input("nickles") 
    dimes= get_input("dimes") 
    quarters= get_input("quarters") 

然後等你的程序

+0

線上的語法錯誤currency = float(raw_input(「Enter%s:」,%(currency))) – Joe 2014-10-31 20:10:14

+0

我是從內存中寫出來的。拿出那個逗號,它應該沒問題。 – anon 2014-10-31 21:01:25