Lesson Code:
"""
User Input and Argparse Playground
"""
import argparse
def get_user_data():
name = input("Please enter your name \n")
fav_vood = input("Please enter your fav food \n")
return name, fav_vood
data = get_user_data()
print(" {} loves to eat {}".format(data[0], data[1]))
parser = argparse.ArgumentParser()
parser.add_argument("input1") # required
parser.add_argument("--input2", required=False) # optional
args = parser.parse_args()
print("required arg is:", args.input1)
print("optional arg is:", args.input2)
def get_user_data():
name = args.input1
fav_vood = args.input2
return name, fav_vood
print(get_user_data())
""" terminal
AlluringMind:~ alluringmind$ cd ~/PycharmProjects/Unit3
AlluringMind:PycharmProjects alluringmind$ cd Unit3
AlluringMind:Unit3 alluringmind$ ls
args_kwargs.py example.txt string_manipulation.py
dictionaries.py file_manipulation.py user_input_argparse.py
error_handling.py functions.py venv
AlluringMind:Unit3 alluringmind$ python user_input_argparse.py
Please enter your name
Bob
Traceback (most recent call last):
File "user_input_argparse.py", line 12, in module
data = get_user_data()
File "user_input_argparse.py", line 7, in get_user_data
name = input("Please enter your name \n")
File "string", line 1, in module
NameError: name 'Bob' is not defined
AlluringMind:Unit3 alluringmind$ python user_input_argparse.py
Please enter your name
"bob"
Please enter your fav food
"beer"
bob loves to eat beer
AlluringMind:Unit3 alluringmind$ python user_input_argparse.py
Please enter your name
^Z
[1]+ Stopped python user_input_argparse.py
AlluringMind:Unit3 alluringmind$ python user_input_argparse.py "Jimmy" --input2 "cheese"
usage: user_input_argparse.py [-h] [--input2 INPUT2] input1
user_input_argparse.py: error: argument input1: invalid int value: 'Jimmy'
AlluringMind:Unit3 alluringmind$ python user_input_argparse.py "Jimmy" --input2 "cheese"
('Jimmy', 'cheese')
AlluringMind:Unit3 alluringmind$ python user_input_argparse.py --input2 "cheese"
usage: user_input_argparse.py [-h] [--input2 INPUT2] input1
user_input_argparse.py: error: too few arguments
AlluringMind:Unit3 alluringmind$
"""
On this page of the site you can watch the video online AlluringMind Python Intro: Program Input Arguments with a duration of hours minute second in good quality, which was uploaded by the user Alluring Mind 25 October 2020, share the link with friends and acquaintances, this video has already been watched 19 times on youtube and it was liked by 1 viewers. Enjoy your viewing!