Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python1 problem set Question3 #69

Open
TheophilusZhang opened this issue May 15, 2020 · 3 comments
Open

Python1 problem set Question3 #69

TheophilusZhang opened this issue May 15, 2020 · 3 comments

Comments

@TheophilusZhang
Copy link

Hello,

I had a question with python 1 problem set Question 3. I think I didn't get the 'sys' part in the example note. I basically copy the code in the example to solve question 3. My code is below:

#!/usr/bin/env python3
import sys
Bo Zhang = sys.argv[1]
Blue = sys.argv[2]
Dreaming = sys.argv[3]
Dog = sys.argv[4]
print(Bo Zhang,Blue,Deaming,Dog)

should these code be saved in a .py file or should it be the code I type in the command line?
I saved these code in a .py script and dont't know what command to use to get the question 3 done.

Thank you,
Bo

@srobb1
Copy link
Collaborator

srobb1 commented May 16, 2020

The idea, is for a user who is running your program to give arguments on the command line. An to not have to open the script and change the values in your code. You then, as a user of your script, can type the script name followed by your custom arguments, like "Bo Zhang" Blue and Dreaming.

ON THE COMMAND LINE run your script like this:
python3 userInput.py "Bo Zhang" Blue Dreaming Dog
python3 userInput.py Sofia green coding goat

Your code should be more generalized using name, color, thing, dog as your variable names. you can now run your code many times providing different values each time.

INSIDE A PY SCRIPT:

#!/usr/bin/env python3
import sys
name = sys.argv[1]
color = sys.argv[2]
thing = sys.argv[3]
animal = sys.argv[4]
print(animal,color,name,thing)

@TheophilusZhang
Copy link
Author

Thanks Sofia, at least I got the command work:) Still a little bit question: why if I only give less than four items in the command like: Blue Dreaming Dog, there will be an error message
Traceback (most recent call last):
File "pyrhon1problemsetQ3.py", line 6, in
animal = sys.argv[4]
IndexError: list index out of range
but if I give 5 items there won't be an error message but only print 4 item.
And another naive question is: does the order matters for the command work? Like name = sys.argv[1] or the order in the print() or the order in the command after the python3 xxx.py?

@srobb1
Copy link
Collaborator

srobb1 commented May 20, 2020

Your code is explicitly asking for 4 items from the command line

name = sys.argv[1]
color = sys.argv[2]
thing = sys.argv[3]
animal = sys.argv[4]

if you only give 3 arguments the code will complain. It tells you that you have an index error, you will learn more about indexes in up coming sections.

And again, you are only printing 4 items

print(animal,color,name,thing)

so only 4 items will be printed. if you only wanted to print the first argument (name = sys.argv[1]) that you supply on the command line you would print like this:

print(name)

if you want to change the order of the output you change the order of the variables:

print(color,animal,thing,name)
print(color,color,color,color)
print(animal,color,name,thing)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants