0

Hello here is my code:

def main():
    #Define Variables
    HomesSold = [0]
    Amount = 0
    HomePrice = [0]
    Amount = GetAmount(Amount)
    HomesSold = AmountHomesSold(Amount)
    #print(HomesSold)


def GetAmount (Amount):
    global amount
    ConvertAmount = float()
    ErrorFlag = False
    Amount = input("Enter the amount of homes sold this year:")
    while not Amount.isdigit():
        Amount = input("Please try again, make sure you are entering a positive number (No commas needed): ")

    print("The amount of houses sold were: ",Amount)
    ConvertAmount = float(Amount)
    return ConvertAmount

def AmountHomesSold(HomePrice):
    HomePrice = 0
    index = 0
    while (index < Amount):
        HomePrice = GetHomePrice()
        HomesSold[index] = HomePrice
        index = index + 1
        print(HomePrice)

    return HomePrice

def GetHomePrice():
    HomePrice = input("How much did the homes sell for?")
    while not HomePrice.isdigit():
        HomePrice = input("Please try again, make sure you are entering a positive number (No commas needed): ")


    return HomePrice

main()

So when I try to set my while statement for index < amount, I keep getting an error saying amount is not defined when it is earlier on in my code. Is there a way I can receive that number?

Flexo
  • 87,323
  • 22
  • 191
  • 272
Atom
  • 5
  • 4
  • scope... you need to pass it in as variable to the function. or make a class and use a class variable. or use a global variable (this is not the best option). the variable is not defined in the scope of that function. it is defined in main and in your GetAmount function. – gloomy.penguin Nov 16 '14 at 05:17
  • How would I receive the return of another function to a separate function without repeating that entire function again – Atom Nov 16 '14 at 05:21
  • in main... `HomesSold = AmountHomesSold(Amount)` and where your function is defined... `def AmountHomeSold(Amount)`... [examples](http://stackoverflow.com/a/16043933/623952) – gloomy.penguin Nov 16 '14 at 05:23
  • 1
    I recommend you to read the entire official python tutorial before trying to program anything by yourself, it's a nice introduction to programming in general, just skip part you dont understand yet, and then reread it later – Ludovic Viaud Nov 16 '14 at 06:01
  • gloomy.penguin solved my problem. Now I have a new issue that I will have to figure out – Atom Nov 16 '14 at 06:16
  • Once your question has been answered please don't change it into another question. If you have further questions work on it for a bit and then ask another separate question later. – Flexo Nov 16 '14 at 06:55
  • Ok, I'm sorry about that – Atom Nov 16 '14 at 06:57

1 Answers1

0

You have to declare "Amount" as a global variable in "AmountHomesSold()" in order to use its value. Otherwise, it will look for a local variable named "Amount" in "AmountHomesSold()" (and there isn't one defined in that function).

Note: I added "global Amount" on the second line to allow the function to use "Amount" as a global variable.

def AmountHomesSold():
    global Amount
    HomePrice = 0
    index = 0
    while (index < Amount):
        HomePrice = GetHomePrice()
        HomesSold[index] = HomePrice
        index = index + 1
        print(HomePrice)

        return HomePrice

For more information, see Use of "global" keyword in Python.

Community
  • 1
  • 1
Vedaad Shakib
  • 739
  • 7
  • 20