Thursday, December 18, 2014

1.3.7 For Loops




Lottery Ticket

import random
def matches(ticket):
winners = []
result = 0
for x in ticket:
winners.append(random.randint(1,60))
z = 0
for y in winners:
if x == y:
z = 1
if z == 1:
result += 1
ticket.sort()
winners.sort()
print 'the winning numbers are ' + str(winners)
if z == 1:
return "your ticket had 1 match" 
if z == 0 or z >=2:
return "your ticket had " + str(result) + "matches"


Hangman

def hangman_display(guessed, secret):
ret = ""
for i in secret:
x = 0
for j in guessed:
if i.lower() in j.lower():
x = 1
if x == 1 or (i in " !,.?"):
ret - ret + i
else:
ret = ret + "-"

return ret

Sometimes code using an iterative loop can be written without a loop, simply repeating the iterated code over and over as separate lines in the program. Explain the disadvantages of developing a program this way.

 Really inefficient and lots of work.

 Name a large collection across which you might iterate.
 A area holding a huge amount of items.


What is the relationship between iteration and the analysis of a large set of data?
A iteration is tasks completed one at a time. Iterative loops analyze huge sets of data.

Thursday, December 11, 2014

1.3.6 Tuples and Lists

import random
def guess_letter():
           alphabet = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z')
print random.choice(alphabet)


def roll_dice():
          a = random.randiant(1,6)
          b = random.randiant(1,6)
           print  a + b
1.       Consider a string, tuple, and list of characters.

In []: a = 'acbde'
In []: b = ('a', 'b', 'c', 'd', 'e')
In []: c = ['a', 'b', 'c', 'd', 'e']

The values of a[3], b[3], and c[3] are all the same. In what ways are a, b, and c different?

'a' uses a string to keep the letters. 'b' is a tuple that holds them. 'c' is a list to keep its values


2.      Why do computer programming languages almost always have a variety of variable types? Why can't everything be represented with an integer?

There is a variety because not all variables can be used in the same situation they might have only a certain one that can be used.

Friday, December 5, 2014

1.3.5 Strings Conclusion



Code:
def how_elgible(essay):
ans = 0
if len(essay) <= 140:
if '"' in essay:
ans = ans + 1
if ',' in essay:
ans = ans + 1
if '!' in essay:
ans = ans + 1
if '?' in essay:
ans = ans + 1
print ans
else:



        print 'Too many characters.'







1.       How many characters are in this sentence? Does it matter whether Python is storing the string as one byte per character or four bytes per character?

there are 41 characters. No it does not matter how many bytes there are per character.

2.      This question asks you about something you have not learned. In fact, the question is asking about details that go beyond what you will learn in this course. However, wondering what is going on at a lower level of abstraction – and talking about it – can be a useful strategy when learning about computing.

Describe what you think occurs in memory when the following code is executed.

In []: a = 'one string'
In []: b = 'another'
In []: c = a[:3] + ' and ' + b
In []:print(c[6:10])

It looks like it will say 'one string and another' then maybe it will print out the characters of 6 - 10.