# Center for Analysis and Design of Intelligent Agents

### Site Tools

public:t-vien-12-1:lab_1_materials

# Differences

This shows you the differences between two versions of the page.

public:t-vien-12-1:lab_1_materials [2012/01/11 11:23]
hannes created
public:t-vien-12-1:lab_1_materials [2012/01/11 14:39] (current)
hannes
Line 5: Line 5:
- **Hello World:**\\ Create a program called **''myhello.py''** that prints "Hello World!" on the screen. \\ \\ <code python>   - **Hello World:**\\ Create a program called **''myhello.py''** that prints "Hello World!" on the screen. \\ \\ <code python>
# HINT: Typical print statement # HINT: Typical print statement
-print "This gets printed!"+print("This gets printed!")
</code> </code>
- **Creating a Function:**\\ Add the function **''greet''** to your program, which takes a person's name as input and prints out a greeting addressed to that person.  Call your new function with your own name from within the program.  \\ \\ <code python>   - **Creating a Function:**\\ Add the function **''greet''** to your program, which takes a person's name as input and prints out a greeting addressed to that person.  Call your new function with your own name from within the program.  \\ \\ <code python>
Line 11: Line 11:
def myfunc(x): def myfunc(x):
""" Prints out the value of x, regardless of type """    """ Prints out the value of x, regardless of type """
-   print x+   print(x)
# Let's use our function to print out the number 3 # Let's use our function to print out the number 3
myfunc(3) myfunc(3)
Line 19: Line 19:
mylist = [1,3,5,'A','B'] mylist = [1,3,5,'A','B']
for x in mylist: for x in mylist:
-   print x+   print(x)
</code> </code>
- **Randomness and Command Line:**\\ Instead of greeting each friend once, make the program greet one at random and repeat this process as many times as you specify on the command line when you launch it.  \\ \\ <code python>   - **Randomness and Command Line:**\\ Instead of greeting each friend once, make the program greet one at random and repeat this process as many times as you specify on the command line when you launch it.  \\ \\ <code python>
Line 25: Line 25:
import random # Import the 'random' functions module import random # Import the 'random' functions module
# Print a single random entry from a list # Print a single random entry from a list
-print random.choice(['A',4,'B','hello'])+print(random.choice(['A',4,'B','hello']))
</code><code python> </code><code python>
# NOTE: 'random.choice' means you're calling 'choice' from  # NOTE: 'random.choice' means you're calling 'choice' from
Line 32: Line 32:
#       from it,  you could also have imported it explicitly first: #       from it,  you could also have imported it explicitly first:
from random import choice from random import choice
-print choice(['A',4,'B','hello'])+print(choice(['A',4,'B','hello']))
</code><code python> </code><code python>
# HINT: Reading a command line argument # HINT: Reading a command line argument
from sys import argv # Import the command line argument list 'argv' from sys import argv # Import the command line argument list 'argv'
if (len(argv)>1):    # Only if some arguments exist... if (len(argv)>1):    # Only if some arguments exist...
-   print argv[1]     # ...print the first argument (They are all strings)+   print(argv[1])    # ...print the first argument (They are all strings)
</code><code python> </code><code python>
# HINT: Converting a string to a number and looping  # HINT: Converting a string to a number and looping
i = int('3') i = int('3')
for j in range(i):  # j = 0, 1, 2  for j in range(i):  # j = 0, 1, 2
-   print j+   print(j)
</code> </code>
- **A Class:**\\ Add the class **''Tool''** to your program.  This class represents a useful object that is made from some particular material.  The problem is that the tool sometimes breaks when it is being used, depending on its material strength.  You will simulate this behavior with this class.     - **A Class:**\\ Add the class **''Tool''** to your program.  This class represents a useful object that is made from some particular material.  The problem is that the tool sometimes breaks when it is being used, depending on its material strength.  You will simulate this behavior with this class.
Line 62: Line 62:

def show(self):    def show(self):
-      print "I have x="+str(self.x)+" and y="+str(self.y)+      print("I have x="+str(self.x)+" and y="+str(self.y))

# Let's creat an instance and call the show method # Let's creat an instance and call the show method
Line 70: Line 70:
# HINT: Creating and using a dictionary (also called a map or a hash table) # HINT: Creating and using a dictionary (also called a map or a hash table)
mydict = {"A":20, "B":30, "C":"Hello"} mydict = {"A":20, "B":30, "C":"Hello"}
-print mydict["B"] # Prints 30, which is the value associated with the key "B"+print(mydict["B"]# Prints 30, which is the value associated with the key "B"
</code> </code>
- **List of Tuples of Objects:**\\ Make your list of friends now contain a list of tuples where the first element is the friend's name like before, but the second element should be a list of 2-3 instanced tools in that friend's possession.  Make your program pick one person at random, greet them, and then ask to borrow one random tool from them and finally try to use that borrowed tool.  As before, have the program repeat this as many times as you specify or until tools have been unsuccessfully used 10 times in a row (you can make the ''**use**'' method return ''**True**'' or ''**False**'' to help you keep track). \\ \\ <code python>   - **List of Tuples of Objects:**\\ Make your list of friends now contain a list of tuples where the first element is the friend's name like before, but the second element should be a list of 2-3 instanced tools in that friend's possession.  Make your program pick one person at random, greet them, and then ask to borrow one random tool from them and finally try to use that borrowed tool.  As before, have the program repeat this as many times as you specify or until tools have been unsuccessfully used 10 times in a row (you can make the ''**use**'' method return ''**True**'' or ''**False**'' to help you keep track). \\ \\ <code python>
Line 78: Line 78:
mytuple = (3, "Hello", MyClass(5), [7, 6, 4]) mytuple = (3, "Hello", MyClass(5), [7, 6, 4])
# Any contained object can be referenced # Any contained object can be referenced
-print mytuple[1]     # Prints "Hello"+print(mytuple[1])    # Prints "Hello"
mytuple[2].show()    # Prints "I have x=5 and y=defaultY" mytuple[2].show()    # Prints "I have x=5 and y=defaultY"
-print mytuple[3][0]  # Prints 7+print(mytuple[3][0]# Prints 7
</code> </code>
- **Searching Lists:**\\ Add the function **''fix''** to your program that takes in a single **''Tool''** and forces its **''working''** value to **''True''**. Declare a list of names, called **''favorites''**, and instead of quitting after 10 failed attempts at using borrowed tools, make the program fix all tools possessed by those that have their name appear on the **''favorites''** list, but not the others.  Print out the names of those that got their tools fixed and let the program borrow a few more tools to see if it can now continue it's habit a bit longer.  \\ \\ <code python>   - **Searching Lists:**\\ Add the function **''fix''** to your program that takes in a single **''Tool''** and forces its **''working''** value to **''True''**. Declare a list of names, called **''favorites''**, and instead of quitting after 10 failed attempts at using borrowed tools, make the program fix all tools possessed by those that have their name appear on the **''favorites''** list, but not the others.  Print out the names of those that got their tools fixed and let the program borrow a few more tools to see if it can now continue it's habit a bit longer.  \\ \\ <code python>
Line 88: Line 88:
for x in listA: for x in listA:
if x in listB:    if x in listB:
-      print str(x)+" is common!"  # Prints "5 is common!, 9 is common!"+      print(str(x)+" is common!"  # Prints "5 is common!, 9 is common!")
</code> </code>
- **EXTRA: Networking and XML:**\\ Give each tool a price in some foreign currency and when you break it, print out its value in ISK. Use an up-to-date currency exchange rate, like the one you can get here in XML format: https://vefafgreidsla.tollur.is/tollalina/gengi/Innflutningur.aspx   \\ \\ <code python>   - **EXTRA: Networking and XML:**\\ Give each tool a price in some foreign currency and when you break it, print out its value in ISK. Use an up-to-date currency exchange rate, like the one you can get here in XML format: https://vefafgreidsla.tollur.is/tollalina/gengi/Innflutningur.aspx   \\ \\ <code python>
Line 102: Line 102:
# We can extract XML elements by name (we get a list, but only take the first match) # We can extract XML elements by name (we get a list, but only take the first match)