Unit 4

Ace your homework & exams now with Quizwiz!

function

- a collection of commands that are given a name. - the name describes what is does. - variables - describe state - functions- define behavior.

while loop

- a way of repeating code - gives us more control over how the program runs - uses conditions like if statements - loops until condition is false

w = "" for i in range(5): w = input("Enter a name: ") print("Name #" + str(i+1) +" "+ w)

- for loop is much easier to control.

num

- num loop control variable - num must change in the loop for the loop to stop

computer models

- represent a real world situation using a computer. - can involve data, graphics and sound. - helps us look at new situations and try to measure how change impacts things examples: - how a new medicine is absorbed - simulation of new jet plane - how temperature changes impact weather. - how a new dam impacts fish populations.

range function

- returns a set of numbers - returns numbers from '0' to 'parameter - 1' - there are three combination of parameters it can have.

range(x)

- returns numbers from 0 to x-1 ex: print(range(15)) - returns: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

range (x,y)

- returns numbers from x to y-1 ex: print(range(2,8) - returns: [2, 3, 4, 5, 6, 7]

simulations

- run experiments using computer models. - make changes and see what happens - used to predict and understand what may happen

mote carlo simulation

- specifically, using random numbers made by computers to test computer models. - developed during world war II while working on the Manhattan project - knew how one neutron would react- testing on a group of neutrons was too dangerous. -use in most experimental science and engineering, gaming

loop control variable

- the variable used to control when the loop stops. - can use any variable, but we often use i, j & k

initialize

- to set a variable to a known value - when using a sum variable we usually set it to 0 -this also lets python set the data type - here it is an integer

sum variable

- variable used to add up a set of numbers - usually initialize it to 0.

tips

- when tracing, you're trying to "break" the code. look for testing values that might cause unexpected results - make sure to try a variety of numbers. what does zero do? negative values?

two ways to end a loop

1. User Input - loop UNTIL.. - Input a value to change loop control variable - do not know how many times this loop will repeat 2. Count variable - ex: print "Coding" ten times - Count to change loop control variable - c = c + 1 - usually know how many times this loop will repeat

algorithms must:

1. have an order 2. have clear instructions 3. be operations that can be on by a computer 4. produce a result 5. stop in a finite amount of time

When do you use a for loop instead of a while loop?

1. when there is a definite starting and ending point 2. when you know how many times you want the loop to run. ex: print the numbers 1 to 10. for i in range (1, 11): print(i)

setting a starting value for a variable is called

Initializing

can every while loop be replaced with a for loop

NO.

loop review

USER CONTROLLED: word = "empty" while (word!="STOP"): word = input ("Enter a word, STOP to end the loop.") print("You entered: " + word) _______________________________________ adding the "c": word = "empty" c = -1 while (word!="STOP"): word = input ("Enter a word, STOP to end the loop.") print("You entered: " + word) c = c + 1 print("You entered: " +str(c) + " words.") _____________________________ ______________________________

monte carlo methods

Using random numbers made by computers to test computer models.

sum variable

Variable used to add up a set of numbers.

for loop

a loop with the count variable built in

loop control variable

a variable whose value determines whether loop execution continues

for i in range(1, 29, 3): print(i, end= " ")

adding will help include the 28.

why build computer models?

advantages: - cost - speed

count loop example

c = 0 while(c <7): print("Python") c = c + 1 __________________________c = 0 while(c <7): print("Python" ,end=" ") c = c + 1 _________________________ loop controlled by a count variable. Usually know exactly how many times the loop will run.

count variable

c = c + 1 - used to count how many times the loop runs - why did we use c = 0 before the loop? c is added to 1 which creates the variable. (c) - initializing variables: sets a starting value for a variable. with loops we initialize before the loop to control the values.

loop

command to repeat code

a for loop is used to replace a ___ while loop

counting

name = input("Enter a name, STOP to end") while(none!= "STOP"): print("You entered: " + name) name = input("Enter a name, STOP to end: ") print("Done.")

loop control variable

max and min

n = int(input("Enter a positive number: ")) max = n while(n!=-1): n = int(input("Enter a positive number: ")) if(max<n): max = n

counting variables

name = input("STOP to end: ") c = 0 while (name != "STOP"): print("You entered: " + name) name = input("STOP to end: ") c = c +1 print("You entered " + str(c) + " words. ")

another example (numbers)

num = int(input("Enter a number, -1 to stop")) sum = 0 while (num != -1): print("You entered: " + str(num)) num = int(input("Enter a number, -1 to stop")) sum = sum + num print("Sum of numbers entered: " +str(sum))

for i in range (40, 19, -5): print(i, end=" ")

outputs 40 35 30 25 20

for n in range (1, 4): print(n)

outputs: 1 2 3

algorithm

precise set of rules for how to solve a problem.

print (range(8))

prints: [0, 1, 2, 3, 4, 5, 6, 7] 8 is a parameter- this is a value sent to a function

tracing code

reading through code to find errors and predict results. - write down variable names - plug in initial values - go through each line of code and write down changes to the variable values - fix any issues in the code, and repeat as needed

modeling

represent a real world situation using a computer.

range(x, y, z)

returns numbers from x to y-1 counting by z ex: print(range 9,0,-2)) - returns: [9,7,5,3,1]

simulation

run experiments using computer models.

summing

sum = 0 for i in range (1, 6): n = int(input("Next number: ")) sum = sum + i print(str(i)+": Adding: " +str(n)+ " Sum: " + str(sum)) print("Average: " + str(1.0*sum/5)) -used to add up a set of numbers - must initialize the variable first sum = 0

Write code using the range function to add up the series 20, 30, 40, ... 90 and print the resulting sum each step along the way. 20 50 90 140 200 270 350 440

sum = 0 for x in range(20,91,10): sum +=x print (sum)

Write code using the range function to add up the series 3, 6, 9, 12, 15, ..... 66 and print the resulting sum. 759

sum = 0 for x in range(3,67,3): sum +=x print(sum)

write a program to add up the prices of ten items a user types in

sum = 0 p = 0 n = int(input("How many items are you buying?")) for in range(10): p = float(input("Enter the price: $")) sum = sum + p tax = sum *.05 print("Total: $" + str(sum)) print("With tax: $" + str(sum +tax))

num = int(input("Enter a number, -1 to stop: ")) while(num != -1): print("You entered: " + str(num)) num = int(input("Enter a number, -1 to stop: ")) print("Done.")

while loop

while vs for loops

x = 0 while (x<=17): print(x) x = x + 2 _______________________ for x in range (0, 18, 2): print

20 21 22 23 24 25 26 27 28 29 30

for i in range(20,31): print(i, end = " ")

write a for loop to make one line of 5,10,15.. 75.

for x in range(5,76,5): print(x, end=" ")

make a for loop 88, 84, 80... 44

for x in range(88,43,-4): print(x, end=" ")

debugging code

http://pythontutor.com/

how to make simulations

in code: - random numbers - repetition

parameter

information sent to a function.

user input example

input numbers until the user types in -1. Find the sum. while (n! = -1): sum = sum + n n = int(input("enter #'s, -1 to stop")) print("Total: " _str(sum)) _____________________ loop controlled by user inputted values. Do not know how many times the loop will run.

have the user enter temps until temp is greater than 32. Count how many values were entered, and then find the average.

temp = int(input("Enter a temperature, 32 or higher to stop: ")) c = 0 sum = 0 #while (temp < 32): #print(temp) sum = sum + temp temp = int(input("Enter a temperature, 32 or higher to stop: ")) c = c + 1 print("You entered " + str(c) + " values.") #print(sum) print("Average temp = " + str(sum/c) + " degrees.")

for r in range (10, 60, 10):print("Hello")

to print text

loop control variable

variable that controls when the loop stops. it must change inside the loop.

Write a loop that inputs words until the user enters STOP. After each input, the program should number each entry and print in this format:

w = input("Please enter the next word: ") x=0 while(w!="STOP"): x=x+1 print("#"+str(x)+ ": You entered " + str(w)) w=input("Please enter the next word: " ) print("All done. "+str(x)+" words entered.")


Related study sets

Chapter 57 | Management of Patients with Burn Injury

View Set

Techniques - Acceptance and Evaluation Criteria

View Set

05. 19th and 20th Century Architecture

View Set

Physical Fitness - Unit 1: Lesson 8. Putting The Pieces Together

View Set