tynation.com

Monday, May 2, 2011

IT104

While Loop:

while (count < 10):

Indeterminate loop. Will loop and not increment its count.

count = 5
while (count < 10):

This code will loop infinitely.
while (statement is true):
     do this code

count = 5
while (count < 10):
     print count

This code will loop infintely.

count = 5
while (count < 10):
     print count
     count = count + 1

This will print 5, 6, 7, 8 and 9
If you make an endless loop, press ctrl-c and that will stop the code

I updated a code from yesterday. Cut out some fat from it:

bugTotal = 0
counter = 0
for counter in range (1, 8):
     print "How many bugs did you collect on day", counter
     bugTotal = bugTotal + input()
print "over", counter, "days you collected", bugTotal, "bugs"

Lab 1 Code
Purpose: a program that takes user finance input and determinese whether or not a budget is met

count = 1
budget = 0
expenses = 0
deficite = 0
overUnder = 0
print "How many expenses did you have this month?"
expenses = input()
print "What is your budget?"
budget = input()
while (count <= expenses):
     print "input expense number", count
     count = count + 1
     deficite = deficite + input()
overUnder = budget - deficite
print "Your budget is $", budget
print "Your expenses totaled $", deficite
if (deficite <= budget):
     print "Your budget has been met"
else:
     print "Your budget has been exceeded"
print "Your over/under is $", overUnder

Lab 2 Code
Purpose: print how far a train will have traveled every hour based on user input for time in hours and speed

distance = 0
time = 0
count = 1
print "How fast is this train going?"
speed = input()
print "For how many hours has the train been traveling?"
time = input()
while (count <= time):
     distance = count * speed
     print "in", count, "hours it travelled", distance, "miles"
     count = count + 1

Homework 1 Code
Purpose: A program that displays a table of C*0-C*20 and displays equivalent F temperatures

count = 0
fTemp = 0
while (count <= 20):
     fTemp = 9 * count / 5 + 32
     print count, "C =", fTemp,"F"
     count = count + 1

Homework 2 Code
Purpose: Create a program that doubles a pay every day and displays a table of how much was earned over a number of days determined by input

pay = .01
total = 0
days = 0
count = 1
print "For how many days does this travesty go on?"
days = input()
print "Day|Total|Rate"
while (count <= days):
     total = total + pay
     print count,"|$",total,"|$",pay
     pay = pay * 2
     count = count + 1


I dont know if I like while loops more or for loops better. Perhaps for loops because there is less code involved.

That's all for now.
-Ty

No comments: