Home > Blockchain >  (Python) I cannot for the life of me understand Scopes and why the variables in them aren't bei
(Python) I cannot for the life of me understand Scopes and why the variables in them aren't bei

Time:12-08

I'm writing a Days Calculator and I'm having an issue with getting my variables inside my function to be read outside of them.

I want the userInput() function to take userInput for the dates. if I put everything under: #-=-=Varibles-=-=

then it works.

but even tho I'm calling the userInput() scope in the main() scope, idle states that the variable doesn't exist and throws an exception.

# Python3 program to find number of days
# between two given dates
from datetime import date

def ask_user(Question):
    VALID = 1
    while VALID == 1:
        try:
            response = int(input(Question))
        except ValueError as e:
            print("\nNOT A VALID ENTRY, TRY AGAIN\n")
            VALID = 1
        else:
            break
    return response
 
def numOfDays(date1, date2):
    return (date2-date1).days

def userInput():
    year1  = ask_user("Enter the year of the first date(yyyy): ")
    month1 = ask_user("Enter the Month of the first date: ")
    day1   = ask_user("Enter the Day of the first date: ")
    print("now the second date")
    year2  = ask_user("Enter the yeay of the second date(yyyy): ")
    month2 = ask_user("Enter the month of the second date: ")
    day2   = ask_user("Enter the Day of the second date: ")
    # print(year1,month1,day1,year2,month2,day2)
    return year1,month1,day1,year2,month2,day2

def calaMath():
    # calculations
    date1 = date(year1, month1, day1)
    date2 = date(year2, month2, day2)
    print(numOfDays(date1, date2), "days")

def main():
    userInput()
    calaMath()

#=-=-=-=-Varibles-=-=-=-=-


main()


CodePudding user response:

You are trying to use local variables declared inside userInput() after they are out of scope. One solution would be to declare all variables globally, but that's not a best practice.

Use the return values from userInput() and pass those into calaMath().

Regarding scope: each set of variables is only visible in the function where they are declared.

  • Inside userInput() you can use year1, month1, day1, year2, month2, and day2.
  • Inside calaMath() you can use yr1, mn1, dy1, yr2, mn2, and dy2.
  • Inside main() you can use y1, m1, d1, y2, m2, and d2.
def userInput():
    year1  = ask_user("Enter the year of the first date(yyyy): ")
    month1 = ask_user("Enter the month of the first date: ")
    day1   = ask_user("Enter the day of the first date: ")
    print("Now the second date")
    year2  = ask_user("Enter the year of the second date(yyyy): ")
    month2 = ask_user("Enter the month of the second date: ")
    day2   = ask_user("Enter the day of the second date: ")
    return year1, month1, day1, year2, month2, day2

def calaMath(yr1, mn1, dy1, yr2, mn2, dy2):
    date1 = date(yr1, mn1, dy1)
    date2 = date(yr2, mn2, dy2)
    print(numOfDays(date1, date2), "days")

def main():
    y1, m1, d1, y2, m2, d2 = userInput()
    calaMath(y1, m1, d1, y2, m2, d2)

CodePudding user response:

Function are like a black box, nothing outside of it can see what is inside, only its output, aka what is returned, to make use of that you need to assign it to a variable (usually) and then work on that, otherwise those are discarded.

The problem here is that you effectively discarded the result of userInput in your main function by not saving it inside some variable and give it to calaMath to do its thing.

You have it right with your ask_user function and how you use it.

There are 2 simply ways to fix it

Capture the result of userInput and give it to calaMath

def main():
   year1,month1,day1,year2,month2,day2 = userInput()
   calaMath(year1,month1,day1,year2,month2,day2)

and of course change calaMath accordingly

def calaMath(year1,month1,day1,year2,month2,day2):
    #the rest stay the same

the other simple fix is simple make the call to userInput inside of calaMath

def calaMath():
   year1,month1,day1,year2,month2,day2 = userInput()
   #the res stay the same

and change main accordingly

def main():
    calaMath()
  • Related