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
  Passing writable arguments to functions. Assembler 11 820 Jan-15-2024, 11:32 PM
Last Post: sgrey
  String int confused janeik 7 1,017 Aug-02-2023, 01:26 AM
Last Post: deanhystad
  I am confused with the key and value thing james1019 3 912 Feb-22-2023, 10:43 PM
Last Post: deanhystad
  Passing string functions as arguments Clunk_Head 3 1,207 Jun-15-2022, 06:00 AM
Last Post: Gribouillis
  Pandas confused DPaul 6 2,467 Sep-19-2021, 06:45 AM
Last Post: DPaul
  is and '==' i'm confused hshivaraj 6 2,626 Sep-15-2021, 09:45 AM
Last Post: snippsat
  Confused with 'flags' tester_V 10 4,788 Apr-12-2021, 03:03 AM
Last Post: tester_V
  Simple Tic Tac Toe but I'm confused Izith 1 2,154 Sep-26-2020, 04:42 PM
Last Post: Larz60+
  I am really confused with this error. Runar 3 2,927 Sep-14-2020, 09:27 AM
Last Post: buran
  redirect url_for passing arguments with the url Leon79 1 1,611 Jul-09-2020, 05:20 PM
Last Post: Leon79

Forum Jump:

User Panel Messages

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