-3

I need to code a get_input function that has loop validation so the number cannot be less than 0 This is the program. ive made this a global function but my instructor told me it was wrong. Making get_input a global function seems to work but i need to use the means of

def get_input():

get_input()

Ive been setting up the global function as get_input = input just because i have no clue how to do what i posted above without geting the error "global name is not defined".

Any help would be greatly appreciated

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()
Joe
  • 1
  • 1
  • Please show one or more of your attempts and we'll be able to help you. – Larry Lustig Oct 31 '14 at 17:47
  • So where *is* your definition of `get_input`? There are no syntax errors in what you've posted. You might find [this](http://stackoverflow.com/q/23294658/3001761) useful. – jonrsharpe Oct 31 '14 at 17:47
  • You have added more explanation but it is still not clear where you are defining `get_input`. It will be easier if you can post the whole programme including the definition of `get_input`. – Stuart Oct 31 '14 at 17:55
  • Just want to let everyone know that i have just gotten into coding about a week ago so i have very little knowledge in this. I am not looking for a answer but more of guidance to the answer so i can learn how to do it. – Joe Oct 31 '14 at 18:00
  • Make sure you define your get_input function before calling `main()`. Then you should not get any error concerning the global name. – Stuart Oct 31 '14 at 18:05

1 Answers1

1

Looks like you need to just put all your raw_input statements inside your get_input function.

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")

Then so on with your program

anon
  • 26
  • 1