Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Push Lesson 5

Signed-off-by: Kellan Jacobs <kellankade@gmail.com>
  • Loading branch information...
commit 2c06e0a7175b8e3d0215d891a8ea4b708e349e30 1 parent d5acad0
@kellanjacobs kellanjacobs authored
View
20 Lesson05/lesson05ex01.py 100644 → 100755
@@ -1,13 +1,15 @@
-''' Prompts the user for their name. Checkes to see if the name matches the stored
-username in var USERNAME. If it does it prints out hello username.
+#!/usr/bin/env python
+'''
+Takes username from a command line argument. Prints Welcome username
'''
-# Set username we are checking for. Change this to your name.
-YOURNAME = 'Kellan'
+# Import argv so we can parse command arguments
+from sys import argv
-# Ask the user to type in their name.
-username = raw_input('Please tell me your name: ')
-# Test if our built in name matches what the user typed.
-if (username == YOURNAME):
- print "Welcome %s" % (username)
+
+if __name__ == '__main__':
+
+ # Unpack command line variables
+ script, username = argv
+ print "Hello {0}".format(username)
View
17 Lesson05/lesson05ex02.py
@@ -0,0 +1,17 @@
+#!/usr/bin/env python
+'''
+Takes username from a command line argument. Prints Welcome username
+Program will deliver an error is the user does not pass in one value.
+'''
+
+# Import argv so we can parse command arguments
+from sys import argv
+
+
+
+if __name__ == '__main__':
+
+ if len(argv) == 2:
+ # Unpack command line variables
+ script, username = argv
+ print "Hello {0}".format(username)
View
19 Lesson05/lesson05ex03.py
@@ -0,0 +1,19 @@
+#!/usr/bin/env python
+'''
+Takes username from a command line argument. Prints Welcome username
+Program will deliver an error is the user does not pass in one value.
+'''
+
+# Import argv so we can parse command arguments
+from sys import argv
+
+
+
+if __name__ == '__main__':
+
+ if len(argv) == 2:
+ # Unpack command line variables
+ script, username = argv
+ print "Hello {0}".format(username)
+ else:
+ print "One argument required you provided {0}".format(len(argv))
View
19 Lesson05/lesson05ex04.py
@@ -0,0 +1,19 @@
+#!/usr/bin/env python
+'''
+There are other types of tests you can do. While we have not covered lists
+yet we can check to see if an item is in a list
+'''
+
+# Import argv so we can parse command arguments
+from sys import argv
+
+
+
+if __name__ == '__main__':
+
+ friends = ['kellan', 'Sam', 'Joe']
+ login = raw_input('Type your login name: ')
+ if login in friends:
+ print "You are logged in"
+ else:
+ print "You are not welcome here"
View
28 docs/lesson05.rst
@@ -40,6 +40,34 @@ Before we get into the main topic of making decisions I want to go over another
script, username = argv
print "Hello {0}".format(username)
+This takes one command line argument and passes it to the value username. Try this example without passing a value or passing more than one value and see what happens.
+
+While there are better ways to fix this error in the future lets use this weeks new skill.
+
+Making Decisions
+================
+In computer programing one way we control our system is by making simple decisions. The method for making decisions is based on writing a simple test. Each test is evaluated for truthfulness. If the test results in a true value we take one path, If not we go in another.
+
+::
+ if SOMETEST:
+ Do something
+
+In our example from above we know that argv needs to have two values in it. So we can test the len of argv and see if it equals 2
+
+::
+ #!/usr/bin/env python
+
+ # Import argv so we can parse command arguments
+ from sys import argv
+
+ if __name__ == '__main__':
+ if len(argv) == 2:
+ # Unpack command line variables
+ script, username = argv
+ print "Hello {0}".format(username)
+
+While this gets rid of our error it is not very helpful. When the user types the wrong number of arguments. It would be better to give the user some feedback.
+
Please sign in to comment.
Something went wrong with that request. Please try again.