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.

No comments:

Post a Comment