Python Forum
Still confused about how passing arguments works
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Still confused about how passing arguments works
#1
I have a simple user input program:

#!/usr/bin/python
#QuickTest.py

def getLocation(x,y,z):
    x = input("Specify horizontal position: ")#latitude
    y = input("Specify vertical position: ")#longitude
    z = input("Specify altitude: ")#above sea level
    return x,y,z

def QuickTestMain():
##    x = 0
##    y = 0
##    z = 0
    getLocation(x,y,z)
    print("You are located at " + str(x) + "degrees latitude, " +
          str(y) + " degrees longitude, " + " and " + str(z) +
          " feet above sea level.")

QuickTestMain()
When lines 11 - 13 are not commented out, it just prints all 0's (I just wanted to initialize them, so that they exist in the program):
Error:
========== RESTART: I:/Python/Python36-32/SamsPrograms/QuickTest.py ========== Specify horizontal position: 3 Specify vertical position: 4 Specify altitude: 5 You are located at 0degrees latitude, 0 degrees longitude, and 0 feet above sea level. >>>
And then if I comment them out, I get:
Error:
========== RESTART: I:/Python/Python36-32/SamsPrograms/QuickTest.py ========== Traceback (most recent call last): File "I:/Python/Python36-32/SamsPrograms/QuickTest.py", line 19, in <module> QuickTestMain() File "I:/Python/Python36-32/SamsPrograms/QuickTest.py", line 14, in QuickTestMain getLocation(x,y,z) NameError: name 'x' is not defined >>>
How do I fix this?
Reply
#2
x,y,z = getLocation(x,y,z)
Reply
#3
There is no need for your getLocation function to have x,y,z arguments, because user will input them after the function is called.
def getLocation():
    x = input("Specify horizontal position: ")#latitude
    y = input("Specify vertical position: ")#longitude
    z = input("Specify altitude: ")#above sea level
    return x,y,z
and then when you are printing it inside QuickTestMain function add
x,y,z = getLocation()
because you need to store the return values from your getLocation function, otherwise they will get lost :D
Reply
#4
As mention bye @mlieqo getLocation() shall not have argument in,they get create in function.
PEP-8 and f-string.
def location():
    x = input("Specify horizontal position: ")
    y = input("Specify vertical position: ")
    z = input("Specify altitude: ")
    return x, y, z

def quick_test():
    latitude,longitude,altitude = location()
    print(f'You are located at {latitude} degrees latitude '
          f'{longitude} degrees longitude and {altitude} feet above sea level.')

quick_test() 
Output:
You are located at 100 degrees latitude 200 degrees longitude and 300 feet above sea level.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pandas confused DPaul 6 210 Sep-19-2021, 06:45 AM
Last Post: DPaul
  is and '==' i'm confused hshivaraj 6 351 Sep-15-2021, 09:45 AM
Last Post: snippsat
  Confused with 'flags' tester_V 10 1,282 Apr-12-2021, 03:03 AM
Last Post: tester_V
  Simple Tic Tac Toe but I'm confused Izith 1 650 Sep-26-2020, 04:42 PM
Last Post: Larz60+
  I am really confused with this error. Runar 3 831 Sep-14-2020, 09:27 AM
Last Post: buran
  redirect url_for passing arguments with the url Leon79 1 656 Jul-09-2020, 05:20 PM
Last Post: Leon79
  passing-arguments-from-one-script-to-another jacklee26 7 1,210 Apr-21-2020, 03:55 PM
Last Post: deanhystad
  Confused by 'break' in the official documents Chuck_Norwich 2 853 Apr-12-2020, 09:26 PM
Last Post: Chuck_Norwich
  Confused on how to go about writing this or doing this... pythonforumuser 3 980 Feb-10-2020, 09:15 AM
Last Post: snippsat
  I always get 'None' returned. Confused. What did I miss? jpezz 2 1,086 Apr-07-2019, 10:06 AM
Last Post: jpezz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020