diff --git a/Lab 01 - Calculator/lab_01_part_a.py b/Lab 01 - Calculator/lab_01_part_a.py index 792d600..4ce89a4 100644 --- a/Lab 01 - Calculator/lab_01_part_a.py +++ b/Lab 01 - Calculator/lab_01_part_a.py @@ -1 +1,9 @@ -# +#!/usr/bin/env python3 +# 1.1 Part A +# Conner Walkenhorst +# 11/2/2017 +"""ask the user for a temperature in Fahrenheit, then print the temperature in Celsius""" + +temperature = int(input('Enter a temperature in Fehrenheit: ')) +celsius = ((temperature - 32)/1.8) +print(celsius) diff --git a/Lab 01 - Calculator/lab_01_part_b.py b/Lab 01 - Calculator/lab_01_part_b.py index 792d600..368a7fd 100644 --- a/Lab 01 - Calculator/lab_01_part_b.py +++ b/Lab 01 - Calculator/lab_01_part_b.py @@ -1 +1,13 @@ -# +#!/usr/bin/env python3 +# 1.2 Part B +# Conner Walkenhorst +# 11/2/2017 +"""ask user for information needed to find area of trapazoid, then print area""" + +print('Area of a trapezoid') +height = int(input('Enter the height of the trapezoid: ')) +lenght_bottom = int(input('Enter the length of the bottom base: ')) +lenght_top = int(input('Enter the length of the top base: ')) +area1 = (lenght_bottom + lenght_top) +area2 = (0.5*area1*height) +print('The area is:',area2) diff --git a/Lab 01 - Calculator/lab_01_part_c.py b/Lab 01 - Calculator/lab_01_part_c.py index 792d600..d51dcac 100644 --- a/Lab 01 - Calculator/lab_01_part_c.py +++ b/Lab 01 - Calculator/lab_01_part_c.py @@ -1 +1,9 @@ -# +#!/usr/bin/env python3 +# 1.3 Part C +# Conner Walkenhorst +# 11/3/2017 +"""calculate the area of square from user input values""" + +length = int(input('Length: ')) +width = int(input('Width: ')) +print('Area of Square: ',length*width) diff --git a/Lab 03 - Create a Quiz/main_program.py b/Lab 03 - Create a Quiz/main_program.py index 792d600..62823f1 100644 --- a/Lab 03 - Create a Quiz/main_program.py +++ b/Lab 03 - Create a Quiz/main_program.py @@ -1 +1,41 @@ -# +#!/usr/bin/env python3 +# Lab 03 - Create a Quiz +# Conner Walkenhorst +# 11/7/2017 + +"""Make my own quiz from scratch""" + +total = 0 +print('Welcome to My Quiz') +answer1 = int(input('Question #1: What is 2+2? ')) +if answer1 == 4: + total += 1 + print(answer1,'is correct.') +else: + print('Sorry',answer1,'is incorrect.') +answer2 = int(input('Question #2: How many letters does the color blue have in its name? ')) +if answer2 == 4: + total += 1 + print(answer2,'is correct.') +else: + print('Sorry',answer2,'is incorrect.') +answer3 = input('Question #3: What is the name of the United States National Anthem? ') +if answer3 == 'The Star-Spangled Banner' or answer3 == 'the star-spangled banner' or answer3 == 'The Star Spangled Banner' or answer3 == 'the star spangled banner': + total += 1 + print(answer3,'is correct.') +else: + print('Sorry',answer3,'is incorrect.') +answer4 = int(input('Question #4: How many letters does the color red have in its name? ')) +if answer4 == 3: + total += 1 + print(answer4,'is correct.') +else: + print('Sorry',answer4,'is incorrect.') +answer5 = input('Question #5: What is the best soda ever made? ') +if answer5 == 'Mountain Dew' or answer5 == 'mountain dew' or answer5 == 'mt. dew' or answer5 == 'mt dew': + total += 1 + print(answer5,'is correct.') +else: + print('Sorry',answer5,'is incorrect.') +percentCorrect = (total/5)*100 +print(str(percentCorrect) + '%') diff --git a/Lab 04 - Camel/main_program.py b/Lab 04 - Camel/main_program.py index 792d600..45c63dc 100644 --- a/Lab 04 - Camel/main_program.py +++ b/Lab 04 - Camel/main_program.py @@ -1 +1,72 @@ -# +#!/usr/bin/env python3 +# Lab 04 Camel +# Conner Walkenhorst +# 11/10/2017 + +"""the user is running from the natives in the great mobi desert on a stolen camel, the user decides what to do""" + +from random import randint +print('Welcome to Camel!') +print('You have stolen a camel to make your way across the great Mobi desert.') +print('The natives want their camel back and are chasing you down! Survive your') +print('desert trek and out run the natives.') +done = False +milesTraveled = 0 +thirst = 0 +camelTiredness = 0 +distanceNativesTraveled = -20 +initialDrinks = 5 +while done is False: + print('A. Drink from your canteen.') + print('B. Ahead moderate speed.') + print('C. Ahead full speed.') + print('D. Stop for the night.') + print('E. Status check.') + print('Q. Quit.') + userChoice = input('What will you do? ') + if userChoice.upper() == 'Q': + done = True + elif userChoice.upper() == 'E': + print('Miles traveled:',milesTraveled) + print('Drinks in canteen:',initialDrinks) + print('The natives are ',distanceNativesTraveled,'miles behind you.') + elif userChoice.upper() == 'D': + camelTiredness = 0 + print('The camel is happy.') + distanceNativesTraveled += 7 + elif userChoice.upper() == 'C': + milesTraveled += randint(10,20) + print('Miles Traveled:',milesTraveled) + thirst += 1 + camelTiredness += randint(1,3) + distanceNativesTraveled += 7 + elif userChoice.upper() == 'B': + milesTraveled += randint(5,12) + print('Miles Traveled:',milesTraveled) + thirst += 1 + camelTiredness += 1 + distanceNativesTraveled += randint(7,14) + elif userChoice.upper() == 'A': + if initialDrinks > 0: + initialDrinks -= 1 + thirst = 0 + else: + print('Error, no drinks left.') + if thirst > 4: + print('You are thirsty.') + elif thirst > 6: + print('You died of thirst.') + done = True + if camelTiredness > 5: + print('Your camel is getting tired.') + elif camelTiredness > 8: + print('Your camel is dead.') + done = True + if distanceNativesTraveled == milesTraveled: + print('The natives caught you.') + done = True + elif distanceNativesTraveled == 15 < milesTraveled: + print('The natives are getting close.') + if milesTraveled == 200: + print('You won.') + done = True diff --git a/Lab 05 - Create a Picture/main_program.py b/Lab 05 - Create a Picture/main_program.py index 792d600..21da2e7 100644 --- a/Lab 05 - Create a Picture/main_program.py +++ b/Lab 05 - Create a Picture/main_program.py @@ -1 +1,91 @@ -# +#!/usr/bin/env python3 +# Lab 03 Creat a Picture +# Conner Walkenhorst +# 11/10/2017 + +"""Create a picture from python code""" + +import pygame + +# Initialize the game engine +pygame.init() + +# Define some colors +BLACK = (0, 0, 0) +WHITE = (255, 255, 255) +BLUE = (0, 0, 255) +GREEN = (0, 255, 0) +RED = (255, 0, 0) + +PI = 3.141592653 + +# Set the height and width of the screen +size = (500, 500) +screen = pygame.display.set_mode(size) + +pygame.display.set_caption("Conner's Python House") + +# Loop until the user clicks the close button. +done = False +clock = pygame.time.Clock() + +# Loop as long as done == False +while not done: + for event in pygame.event.get(): # User did something + if event.type == pygame.QUIT: # If user clicked close + done = True # Flag that we are done so we exit this loop + + # All drawing code happens after the for loop and but + # inside the main while not done loop. + + # Clear the screen and set the screen background + screen.fill(BLACK) + + # Draw a rectangle for grass + pygame.draw.rect(screen, GREEN, [0, 400, 500, 100], 0) + + # Draw a rectangle for house + pygame.draw.rect(screen, BLUE, [150, 200, 200, 200], 0) + + #draw a rectangle for door + pygame.draw.rect(screen, RED, [200, 400, 100, -95], 0) + + #draw an ellipse for door knob + pygame.draw.ellipse(screen, BLACK, [265, 335, 15, 15], 0) + + # This draws a triangle for the house using the polygon command + pygame.draw.polygon(screen, RED, [[250, 75], [150, 200], [350, 200]], 0) + + # Draw an ellipse, using a rectangle as the outside boundaries + pygame.draw.ellipse(screen, BLUE, [200, 130, 100, 50], 0) + + #draw an ellipse for the moon + pygame.draw.ellipse(screen, WHITE, [60, 60, 60, 60], 0) + + #draw a line for upper window + pygame.draw.line(screen, BLACK, [250, 130], [250, 177], 2) + + #draw other line for upper window + pygame.draw.line(screen, BLACK, [200, 150], [300, 150], 2) + + # Select the font to use, size, bold, italics + font = pygame.font.SysFont('Calibri', 25, True, False) + + # Render the text. "True" means anti-aliased text. + # Black is the color. This creates an image of the + # letters, but does not put it on the screen + text = font.render("Hello World", True, BLACK) + + # Put the image of the text on the screen at 250x250 + screen.blit(text, [200, 250]) + + # Go ahead and update the screen with what we've drawn. + # This MUST happen after all the other drawing commands. + pygame.display.flip() + + # This limits the while loop to a max of 60 times per second. + # Leave this out and we will use all CPU we can. + clock.tick(60) + +# Be IDLE friendly +pygame.quit() diff --git a/Lab 06 - Loopy Lab/part_1.py b/Lab 06 - Loopy Lab/part_1.py index 8b13789..135b816 100644 --- a/Lab 06 - Loopy Lab/part_1.py +++ b/Lab 06 - Loopy Lab/part_1.py @@ -1 +1,19 @@ +#!/usr/bin/env python3 +# Lab 06 Loopy Lab +# Conner Walkenhorst +# 11/15/2017 +"""Lab 06 part 1""" + +# Starting Value at 10 +start = 10 + +# Do 9 times +for i in range(9): + # Loop to print digits + for j in range(i + 1): + # Prints starting variable and then adds 1 to it. + print(start, end = ' ') + start += 1 + # Prints new line + print() \ No newline at end of file diff --git a/Lab 06 - Loopy Lab/part_2.py b/Lab 06 - Loopy Lab/part_2.py index 792d600..aec3fb2 100644 --- a/Lab 06 - Loopy Lab/part_2.py +++ b/Lab 06 - Loopy Lab/part_2.py @@ -1 +1,20 @@ -# +#!/usr/bin/env python3 +# Lab 06 Loopy Lab +# Conner Walkenhorst +# 11/15/2017 + +"""Lab 06 part 2""" + +#input for user to say how many o's to print +numberOs = int(input("How many o's do you want? ")) +spaces = ((numberOs*2) - 2) +#loop for o's on top corresponding to user's input +for i in range(numberOs): + print('o'*2, end = '') +print() +#loop for o's on left side corresponding to user's input +for j in range(1, numberOs - 1): + print('o' + (' ' * spaces) +'o') +#loop for o's on bottom corresponding to user's input +for row in range(numberOs): + print('o'*2, end = '') \ No newline at end of file diff --git a/Lab 06 - Loopy Lab/part_3.py b/Lab 06 - Loopy Lab/part_3.py index 792d600..7f17482 100644 --- a/Lab 06 - Loopy Lab/part_3.py +++ b/Lab 06 - Loopy Lab/part_3.py @@ -1 +1,14 @@ -# +#!/usr/bin/env python3 +# Lab 06 Loopy Lab +# Conner Walkenhorst +# 11/13/2017 + +"""Lab 06 programs""" + +#Do this 10 times +for i in range(10): + #print 10 *'s on 1 line + for j in range(10): + print('*',end=' ') + #print a new line + print() \ No newline at end of file diff --git a/Lab 06 - Loopy Lab/part_4.py b/Lab 06 - Loopy Lab/part_4.py index 792d600..317d758 100644 --- a/Lab 06 - Loopy Lab/part_4.py +++ b/Lab 06 - Loopy Lab/part_4.py @@ -1 +1,101 @@ -# +#!/usr/bin/env python3 +# Lab 06 Loopy Lab +# Conner Walkenhorst +# 11/13/2017 + +"""Lab 06 programs""" +""" + Pygame base template for opening a window + + Sample Python/Pygame Programs + Simpson College Computer Science + http://programarcadegames.com/ + http://simpson.edu/computer-science/ + + Explanation video: http://youtu.be/vRB_983kUMc +""" + +import pygame + +# Define some colors +BLACK = (0, 0, 0) +WHITE = (255, 255, 255) +GREEN = (0, 255, 0) +RED = (255, 0, 0) + +pygame.init() + +# Set the width and height of the screen [width, height] +size = (700, 500) +screen = pygame.display.set_mode(size) + +pygame.display.set_caption("My Game") + +# Loop until the user clicks the close button. +done = False +""" +Pygame base template for opening a window + +Sample Python/Pygame Programs +Simpson College Computer Science +http://programarcadegames.com/ +http://simpson.edu/computer-science/ + +Explanation video: http://youtu.be/vRB_983kUMc +""" + +import pygame + +# Define some colors +BLACK = (0, 0, 0) +WHITE = (255, 255, 255) +GREEN = (0, 255, 0) +RED = (255, 0, 0) +BLUE = (31, 244, 255) + +pygame.init() + +# Set the width and height of the screen [width, height] +size = (700, 500) +screen = pygame.display.set_mode(size) + +pygame.display.set_caption("My Game") + +# Loop until the user clicks the close button. +done = False + +# Used to manage how fast the screen updates +clock = pygame.time.Clock() + +# -------- Main Program Loop ----------- +while not done: + # --- Main event loop + for event in pygame.event.get(): + if event.type == pygame.QUIT: + done = True + + # --- Game logic should go here + + # --- Screen-clearing code goes here + + # Here, we clear the screen to white. Don't put other drawing commands + # above this, or they will be erased with this command. + + # If you want a background image, replace this clear with blit'ing the + # background image. + screen.fill(BLACK) + + # --- Drawing code should go here + for y in range(0, 500, 20): + for x in range(0, 700, 20): + pygame.draw.rect(screen, BLUE, (x, y, 10, 10)) + + # --- Go ahead and update the screen with what we've drawn. + pygame.display.flip() + + # --- Limit to 60 frames per second + clock.tick(60) + +# Close the window and quit. +pygame.quit() + diff --git a/Lab 07 - Adventure/main_program.py b/Lab 07 - Adventure/main_program.py index 792d600..4cf14b6 100644 --- a/Lab 07 - Adventure/main_program.py +++ b/Lab 07 - Adventure/main_program.py @@ -1 +1,75 @@ -# +#!/usr/bin/env python3 +# Lab 7: Adventure +# Conner Walkenhorst +# 11/20/2017 + +"""Create a text adventure game using python""" + +room_list = [] +room = ["The Entryway", None, None, 3, None] +room_list.append(room) +room = ["Office Room", None, None, 4, None] +room_list.append(room) +room = ["Ballpit Room", None, None, 5, None] +room_list.append(room) +room = ["West Hall", 0, 4, 6, None] +room_list.append(room) +room = ["Center Hall", 1, 5, 7, 3] +room_list.append(room) +room = ["East Hall", 2, None, 8, 4] +room_list.append(room) +room = ["Weight Room", 3, None, None, None] +room_list.append(room) +room = ["Blade Room", 4, None, None, None] +room_list.append(room) +room = ["Balloon Room", 5, None, None, None] +room_list.append(room) + +done = False +current_room = 0 + +while done == False: + print() + print(room_list[current_room][0]) + #print exits + if room_list[current_room][1] != None: + print("\tThere is an exit to the North.") + if room_list[current_room][2] != None: + print("\tThere is an exit to the East.") + if room_list[current_room][3] != None: + print("\tThere is an exit to the South.") + if room_list[current_room][4] != None: + print("\tThere is an exit to the West.") + f + user_choice = input("Where would you like to go? ") + + if user_choice.lower()[0] == "n": + next_room = room_list[current_room][1] + if next_room == None: + print("You can't go that way.") + else: + current_room = next_room + + elif user_choice.lower()[0] == "e": + next_room = room_list[current_room][2] + if next_room == None: + print("You can't go that way.") + else: + current_room = next_room + + elif user_choice.lower()[0] == "s": + next_room = room_list[current_room][3] + if next_room == None: + print("You can't go that way.") + else: + current_room = next_room + + elif user_choice.lower()[0] == "w": + next_room = room_list[current_room][4] + if next_room == None: + print("You can't go that way.") + else: + current_room = next_room + + else: + print("say again?") diff --git a/Worksheets/worksheet_01.txt b/Worksheets/worksheet_01.txt index 728ad02..80997f7 100644 --- a/Worksheets/worksheet_01.txt +++ b/Worksheets/worksheet_01.txt @@ -6,23 +6,24 @@ and punctuation. Please limit the length of each line to 80 characters. 1. Write a line of code that will print your name. - + print('Conner') 2. How do you enter a comment in a program? - +use # before the comment 3. What do the following lines of code output? ALSO: Why do they give a different answer? print(2 / 3) + 0.66 print(2 // 3) - + 0 4. Write a line of code that creates a variable called pi and sets it to an appropriate value. - +pi = 3.14 5. Why does this code not work? A = 22 print(a) - + Because the A variable is different than the a variable called to print. 6. All of the variable names below can be used. But which ONE of these is the better variable name to use? @@ -34,6 +35,8 @@ area_of_rectangle Area_Of_Rectangle + area_of_rectangle + 7. Which of these variables names are not allowed in Python? (More than one might be wrong. Also, this question is not asking about improper names, just names that aren't allowed. Test them if you aren't sure.) @@ -43,11 +46,11 @@ APPLE Apple2 1Apple - account number + account number this isn't allowed account_number account.number accountNumber - account# + account# this isn't allowed pi PI fred @@ -58,17 +61,18 @@ great.big.variable 2x x2x - total% - #left + total% this isn't allowed + #left this isn't allowed 8. Why does this code not work? print(a) a = 45 - + Because it's telling the program to print something that hasn't been defined yet, then defining it. 9. Explain the mistake in this code: pi = float(3.14) + the number 3.14 is already a float, it doesn't need a float in front of it 10. This program runs, but the code still could be better. Explain what is wrong with the code. @@ -78,68 +82,69 @@ pi = x area = pi * radius ** 2 print(area) - + You don't need to assign x to 3.14 to assign pi to 3.14(or x), you could just assign pi to 3.14 11. Explain the mistake in the following code: x = 4 y = 5 a = ((x) * (y)) print(a) - + You don't need the extra parenthesis around each variable (x and y). 12. Explain the mistake in the following code: x = 4 y = 5 a = 3(x + y) print(a) - + the 'int' object is not callable. 13. Explain the mistake in the following code: radius = input(float("Enter the radius:")) - + could not convert string to float, use of " instead of ' 14. Do all these print the same value? Which one is better to use and why? print(2/3+4) print(2 / 3 + 4) print( 2 / 3+ 4 ) - + Yes, print(2 / 3 + 4) 15. What is a constant? - +a values that cannot be altered by the program. 16. How are variable names for constants different than other variable names? - +constant variable names are immutable. 17. What is a single quote and what is a double quote? Give and label an example of both. - +'hello' +"hello" 18. Write a Python program that will use escape codes to print a double-quote and a new line using the Window's standard. (Note: I'm asking for the Window's standard here. Look it up out of Chapter 1.) - +print("I want to print a double quote \"\n for some reason.") 19. Can a Python program print text to the screen using single quotes instead of double quotes? - +Yes, print("I want to print a double quote \'\n for some reason.") 20. Why does this code not calculate the average? print(3 + 4 + 5 / 3) - + It does in this specific situation, with these numbers. 21. What is an ``operator'' in Python? - +special symbols that carry out arithmetic or logical computation. 22. What does the following program print out? x = 3 x + 1 print(x) - + SyntaxError: multiple statements found while compiling a single statement 23. Correct the following code: user_name = input("Enter your name: )" - + user_name = input("Enter your name: ") 24. Correct the following code: value = int(input(print("Enter your age"))) - + value = int(input("Enter your age: ")) diff --git a/Worksheets/worksheet_02.txt b/Worksheets/worksheet_02.txt index abe4cd7..42e5020 100644 --- a/Worksheets/worksheet_02.txt +++ b/Worksheets/worksheet_02.txt @@ -6,37 +6,37 @@ 1. Give an example of a binary number. (While a number such as ``1'' can be a binary, decimal, and hexadecimal number, try coming up with an example that better illustrates the differences between the different bases of numbers.) - +101101 2. Give an example of a decimal number. - +82 3. Give an example of a hexadecimal number. - +3FA 4. Convert the numbers 1, 10, 100, 1000, and 10000 from binary to decimal. - +1 = 1, 10 = 2, 100 = 4, 1000 = 8, 10000 = 16 5. What is a compiler? - +Converts source code to machine code 6. What is source code? - +The program the developer types into the computer 7. What is machine language? (Don't just say binary. That's not correct.) - +The native code the computer runs 8. What is a first generation language? (Don't just say binary. That's not correct.) - +Machine Language 9. What is a second generation language? - +Assembly Language 10. What is a third generation language? (Explain, don't just give one example.) - +Language like Python or C that has logical structures 11. What is an interpreter and how does it differ from a compiler? - +an interpreter translates a program one statement at a time, a compiler scans the entire program and translates it as a whole into machine code. 12. Search the web and find some of the most popular programming languages. List the website(s) you got the information from and what the languages are. - +Java, Python, C, website: https://www.inc.com/larry-kim/10-most-popular-programming-languages-today.html 13. Look at the job boards and see what languages people are looking for. List the languages and the job board you looked at. - +Java, website: https://www.indeed.com/q-Software-Engineer-l-Missouri-jobs.html 14. What is the difference between the ``syntax'' and ``semantics'' of a language? - - 15. Pick a piece of technology, other than a computer you use regularly. Briefly +syntax refers to formal rules governing the construction of valid statements in a language, semantics refers to the set of rules which give the meaning of a statement. + 15. Pick a piece of technology, other than a computer, that you use regularly. Briefly describe the hardware and software that run on it. - +Cell Phone: a cell phone uses a touch screen to interact with the user through touch and sight, applications serve as software that run on the device. diff --git a/Worksheets/worksheet_03.txt b/Worksheets/worksheet_03.txt index bc85b84..465904e 100644 --- a/Worksheets/worksheet_03.txt +++ b/Worksheets/worksheet_03.txt @@ -10,14 +10,26 @@ print("It is hot outside.") else: print("It is not hot out.") - + the second parenthesis on the end of the first line 2. Write a Python program that will take in a number from the user and print if it is positive, negative, or zero. Use a proper if/elif/else chain, don't just use three if statements. +\ +number = int(input('Enter a number: ')) +if number < 0: + print('Number is negative') +elif number > 0: + print('Number is posative') +elif number == 0: + print('Number is zero') 3. Write a Python program that will take in a number from a user and print out ``Success'' if it is greater than -10 and less than 10, inclusive. (1 pt) +number = int(input('Enter a number: ')) +if -11 < number < 11: + print('Success') + 4. This runs, but there is something wrong. What is it? (1 pt) user_input = input("A cherry is a: ") @@ -27,7 +39,9 @@ print("Correct!") else: print("Incorrect.") - + + the ".upper" is not needed, it will only count a capital "A" as correct without it anyway. + 5. There are two things wrong with this code that tests if x is set to a positive value. One prevents it from running, and the other is subtle. Make sure the if statement works no matter what x is set to. @@ -39,12 +53,16 @@ else: print("x is not positive.") + there can't be a double equal sign in the assigning line of code. also as long as x is assigned to 4 the output can never print "x is not posirive." + 6. What three things are wrong with the following code? (3 pts) x = input("Enter a number: ") if x = 3 print("You entered 3") + there is no colon at the end of the if statement, "x" is a terrible variable name!, the print statement will not print the user's input like it should it will only print "You entered 3". + 7. There are four things wrong with this code. Identify all four issues. (4 pts) answer = input("What is the name of Dr. Bunsen Honeydew's assistant? ") @@ -53,13 +71,17 @@ else print("Incorrect! It is Beaker.") + else statement doesn't have a colon at the end of it. a is not defined, answer needs to be spelled out fully. there needs to be a double equals sign after what should be answer and before "Beaker" in the if statement. and the else statement needs to be backed up the same indentation as the if statement. + 8. This program doesn't work correctly. What is wrong? (1 pt) x = input("How are you today?") if x == "Happy" or "Glad": print("That is good to hear!") - 9. Look at the code below. Write you best guess here on what it will print. + the colon brings up a syntax error. + + 9. Look at the code below. Write your best guess here on what it will print. Next, run the code and see if you are correct. Clearly label your guess and the actual answer. Also, if this or any other example results in an error, make sure to @@ -79,6 +101,8 @@ if z: print("Buzz") + i think it will print "x= 5, y= 5, z= 5, Buzz". what it acually prints "x= 5, y= False, z= True, Buzz" + 10. Look at the code below. Write you best guess on what it will print. Next, run the code and see if you are correct. (2 pts) @@ -97,6 +121,9 @@ print(x == 5 and y == 5) print(x == 5 or y == 5) + i think it will print "True, False, False, False, True, False, False, True, False, True" + it actually prints "True, False, True, False, False, True, False, False, True, False, True" + 11. Look at the code below. Write you best guess on what it will print. Next, run the code and see if you are correct. (2 pts) @@ -110,6 +137,7 @@ print( (2 == 2) == True ) print(3 < "3") + True False True True True False False True 12. What things are wrong with this section of code? The programmer wants to set the money variable according to @@ -130,6 +158,6 @@ else if user_input = C: money = 50 - + not using double equal signs, the user might put a lowercase letter, no space after user_input question, uses else if instead of elif and else diff --git a/Worksheets/worksheet_04.txt b/Worksheets/worksheet_04.txt index 341b2fa..76c67f4 100644 --- a/Worksheets/worksheet_04.txt +++ b/Worksheets/worksheet_04.txt @@ -14,17 +14,39 @@ 1. Write a Python program that will use a for loop to print your name 10 times, and then the word ``Done'' at the end. +name = input('Enter Your Name: ') +for number in range(10): + print(name) +print('Done') + + 2. Write a Python program that will use a for loop to print ``Red'' and then ``Gold'' 20 times. (Red Gold Red Gold Red Gold... all on separate lines. Don't use \n.) +for i in range(20): + print('red') + print('gold') + + 3. Write a Python program that will use a for loop to print the even numbers from 2 to 100, inclusive. +for i in range(2,201,2): + print(i) + + 4. Write a Python program that will use a while loop to count from 10 down to, and including, 0. Then print the words ``Blast off!'' Remember, use a WHILE loop, don't use a FOR loop. +launch = 10 +number = launch +while number > -1: + print(number) + number = number - 1 +print('Blast Off!') + 5. There are three things wrong with this program. List each. (3 pts) print("This program takes three numbers and returns the sum.") @@ -35,12 +57,20 @@ total = total + i print("The total is:", x) + for one, 'x' is a terrible variable name, it does not describe what it is/does at all. where total is assigned to total + i, i should be x. in the last print statement where it calls to print x, it should print total instead. + 6. Write a program that prints a random integer from 1 to 10 (inclusive). +from random import randint +print(randint(1,10)) + 7. Write a program that prints a random floating point number somewhere between 1 and 10 (inclusive). Do not make the mistake of generating a random number from 0 to 10 instead of 1 to 10. +import random +print(random.uniform(1.0,10.0)) + 8. Write a Python program that will: (3 pts) * Ask the user for seven numbers @@ -49,6 +79,25 @@ and the number of negative entries. Use an if, elif, else chain, not just three if statements. + total = 0 +totalPositive = 0 +totalNegative = 0 +totalZero = 0 +number = input('Enter seven numbers: ') +number = number.split() +for num in number: + total = total+int(num) + if int(num) > 0: + totalPositive += 1 + elif int(num) < 0: + totalNegative += 1 + else: + totalZero += 1 +print('Total:',total) +print('Total Positive:',totalPositive) +print('Total Negative:',totalNegative) +print('Total Zero:',totalZero) + 9. Coin flip tosser: (4 pts) * Create a program that will print a random 0 or 1. @@ -57,6 +106,20 @@ * Add a loop so that the program does this 50 times. * Create a running total for the number of heads flipped, and the number of tails. + import random + numberHeads = 0 +numberTails = 0 +for i in range(50): + number = random.randrange(0, 2) + if number == 1: + numberHeads += 1 + print('Heads') + elif number == 0: + numberTails += 1 + print('Tails') +print('Heads:', numberHeads) +print('Tails:', numberTails) + 10. Write a program that plays rock, paper, scissors: (4 pts) * Create a program that randomly prints 0, 1, or 2. @@ -66,4 +129,30 @@ * (It will be easier if you have them enter 1, 2, or 3.) * Add conditional statement to figure out who wins. - + from random import randint +options = [1,2,3] +computerPlayer = options[randint(0,2)] +player = False +while player == False: + player = int(input('Rock = 1, Paper = 2, Scissors = 3. Enter your choice: ')) + if player == computerPlayer: + print('Tie, no one wins.') + elif player == 1: + if computerPlayer == 2: + print('You lose.',computerPlayer, 'beats',player) + else: + print('You win.',player,'beats',computerPlayer) + elif player == 2: + if computerPlayer == 3: + print('You lose.',computerPlayer,'beats',player) + else: + print('You win.',player,'beats',computerPlayer) + elif player == 3: + if computerPlayer == 1: + print('You lose.',computerPlayer,'beats',player) + else: + print('You win.',player,'beats',computerPlayer) + else: + print('That is not a valid choice, please try again.') + player = False + computerPlayer = options[randint(0,2)] diff --git a/Worksheets/worksheet_05.txt b/Worksheets/worksheet_05.txt index 275a18a..c7cabda 100644 --- a/Worksheets/worksheet_05.txt +++ b/Worksheets/worksheet_05.txt @@ -8,39 +8,61 @@ 1. Explain how the computer coordinate system differs from the standard Cartesian coordinate system. There are two main differences. List both. +The computer coordinate system has point (0,0) in the top left corner, +while the standard cartesian coordinate system has point (0,0) in the center. +The computer coordinate system also goes up in x and y values by the more down and right the point goes, +while the standard cartesian coordinate system goes up in x and y values by the more up and right the point goes. + 2. Before a Python Pygame program can use any functions like pygame.display.set_mode(), what two lines of code must occur first? +import pygame and pygame.init() + 3. Explain how WHITE = (255, 255, 255) represents a color. +Because this format represents (red, green, blue) values. If you have full red, full green, and full blue values +than the resulting color would be white due to white being the absolution of color. + 4. When do we use variable names for colors in all upper-case, and when do we use variable names for colors in all lower-case? (This applies to all variables, not just colors.) +If the variable is a constant than it will be all uppercase lettering. + 5. What does the pygame.display.set_mode() function do? +Pull up a pygame window on screen. + 6. What does this for event in pygame.event.get() loop do? +This gets an event. + 7. What is pygame.time.Clock used for? +Contains routines to help keep track of time. + 8. For this line of code: (3 pts) pygame.draw.line(screen, GREEN, [0, 0], [100, 100], 5) - * What does screen do? - * What does [0, 0] do? - * What does [100, 100] do? - * What does 5 do? + * What does screen do? Screen tells the program where to execute + * What does [0, 0] do? [0, 0] sets the starting point + * What does [100, 100] do? [100, 100] sets the ending point + * What does 5 do? 5 tells the z axis value 9. What is the best way to repeat something over and over in a drawing? +void draw() + 10. When drawing a rectangle, what happens if the specified line width is zero? +Then there will not be a border around the rectangle. + 11. Describe the ellipse drawn in the code below. - * What is the x, y of the origin coordinate? - * What does the origin coordinate specify? The center of the circle? - * What is the length and the width of the ellipse? + * What is the x, y of the origin coordinate? x = 20 y = 20 + * What does the origin coordinate specify? The center of the circle? The upper left corner + * What is the length and the width of the ellipse? Length = 100 Width = 250 pygame.draw.ellipse(screen, BLACK, [20, 20, 250, 100], 2) @@ -48,22 +70,36 @@ 12. When drawing an arc, what additional information is needed over drawing an ellipse? +Start and end angles, which are radians. + 13. Describe, in general, what are the three steps needed when printing text to the screen using graphics? +Create a variable that holds information about font to be used, create an image of the text, +tell where the image is to be stamped. + 14. When drawing text, the first line of the three lines needed to draw text should actually be outside the main program loop. It should only run once at the start of the program. Why is this? You may need to ask. +Because it only needs to run once at the start of the program, it doesn't hold things that change. + 15. What are the coordinates of the polygon that the code below draws? pygame.draw.polygon(screen, BLACK, [[50,100],[0,200],[200,200],[100,50]], 5) + [50,100],[0,200],[200,200],[100,50] + 16. What does pygame.display.flip() do? +Updates the display 'flipping' the new graphics to the screen. + 17. What does pygame.quit() do? +Properly shuts down a pygame program. + 18. Look up on-line how the pygame.draw.circle works. Get it working and paste a working sample here. I only need the one line of code that draws the circle, but make sure it is working by trying it out in a full working program. +pygame.draw.circle(screen, (50,50), 5, 1) diff --git a/Worksheets/worksheet_06.txt b/Worksheets/worksheet_06.txt index 35e5f13..c043bb8 100644 --- a/Worksheets/worksheet_06.txt +++ b/Worksheets/worksheet_06.txt @@ -18,6 +18,8 @@ print(x) x = x + 2 + It will print 0 2 4 6 8 all on separate lines + 2. What does this program print out? x = 1 @@ -25,6 +27,8 @@ print(x) x = x * 2 + It will print 1 2 4 6 8 16 32 all on separate lines + 3. Why is the and x >= 0 not needed? x = 0 @@ -32,6 +36,8 @@ print(x) x = x + 2 + Because there is no way for x to go down in value anyway. + 4. What does this program print out? (0 pts) Explain. (1 pt) x = 5 @@ -41,6 +47,8 @@ print("Blast off!") x = x - 1 + It will print 5 4 3 2 1 0, an integer will never be equal to a string, so "Blast Off!" will never print. + 5. Fix the following code so it doesn't repeat forever, and keeps asking the user until he or she enters a number greater than zero: (2 pts) @@ -49,6 +57,11 @@ while x <= 0: print("Too small. Enter a number greater than zero: ") + x = float(input("Enter a number greater than zero: ")) + +while x <= 0: + x = int(input("Too small. Enter a number greater than zero: ")) + 6. Fix the following code: x = 10 @@ -59,6 +72,14 @@ print("Blast-off") + x = 10 + +while x > 0: + print(x) + x = x - 1 + +print("Blast-off") + 7. What is wrong with this code? It runs but it has unnecessary code. Find all the unneeded code. Also, answer why it is not needed. (1 pt) @@ -67,6 +88,8 @@ print(i) i += 1 + The line of code "i += 1" is not needed due to the fact that the for loop and print statement alone will do the same. + 8. Explain why the values printed for x are so different. (2 pts) # Sample 1 @@ -85,4 +108,4 @@ x += 1 print(x) - + Because Sample 1 both for loops are at the same indentation, which means that for loop i will execute, then for loop j will execute. On Sample 2 for loop j is indented into for loop i, so the loop in total will run 10 times more than Sample 1. diff --git a/Worksheets/worksheet_06_codeTester.py b/Worksheets/worksheet_06_codeTester.py new file mode 100644 index 0000000..cd609df --- /dev/null +++ b/Worksheets/worksheet_06_codeTester.py @@ -0,0 +1,15 @@ +# Sample 1 +x = 0 +for i in range(10): + x += 1 +for j in range(10): + x += 1 +print(x) + + # Sample 2 +x = 0 +for i in range(10): + x += 1 + for j in range(10): + x += 1 +print(x) \ No newline at end of file diff --git a/Worksheets/worksheet_07.txt b/Worksheets/worksheet_07.txt index a87ff3f..6ca034e 100644 --- a/Worksheets/worksheet_07.txt +++ b/Worksheets/worksheet_07.txt @@ -9,6 +9,8 @@ 1. List the four types of data we've covered, and give an example of each: + String, Ex: "hello". Integer, Ex: 1. Floating point, 3.145. Boolean, True. + 2. What does this code print out? For this and the following problems, make sure you understand WHY it prints what it does. You don't have to explain it, but if you don't understand why, make sure to ask. @@ -19,12 +21,16 @@ print(my_list[4]) print(my_list[5]) + 2, 101, and IndexError: list index out of range + 3. What does this code print out? my_list=[5, 2, 6, 8, 101] for my_item in my_list: print(my_item) + 5, 2, 6, 8, 101 + 4. What does this code print out? my_list1 = [5, 2, 6, 8, 101] @@ -34,6 +40,8 @@ my_list2[2] = 10 print(my_list2) + [5, 2, 6, 10, 101], and TypeError: 'tuple' object does not support item assignment + 5. What does this code print out? my_list = [3 * 5] @@ -41,6 +49,8 @@ my_list = [3] * 5 print(my_list) + [15], and [3, 3, 3, 3, 3]. + 6. What does this code print out? my_list = [5] @@ -48,6 +58,8 @@ my_list.append(i) print(my_list) + [5, 0, 1, 2, 3, 4] + 7. What does this code print out? print(len("Hi")) @@ -56,18 +68,24 @@ print(len("2")) print(len(2)) + 2, 9, 8, 1, and TypeError: object of type 'int' has no len() + 8. What does this code print out? print("Simpson" + "College") print("Simpson" + "College"[1]) print( ("Simpson" + "College")[1] ) + SimpsonCollege, Simpsono, and i + 9. What does this code print out? word = "Simpson" for letter in word: print(letter) + S, i, m, p, s, o, n all on their own lines + 10. What does this code print out? word = "Simpson" @@ -75,17 +93,23 @@ word += "College" print(word) + SimpsonCollegeCollegeCollege + 11. What does this code print out? word = "Hi" * 3 print(word) + HiHiHi + 12. What does this code print out? my_text = "The quick brown fox jumped over the lazy dogs." print("The 3rd spot is: " + my_text[3]) print("The -1 spot is: " + my_text[-1]) + The 3rd spot is:, and The -1 spot is: . both on separate lines. + 13. What does this code print out? s = "0123456789" @@ -93,10 +117,18 @@ print(s[:3]) print(s[3:]) + 1, 012, 3456789 all on separate lines. + 14. Write a loop that will take in a list of five numbers from the user, adding each to an array. Then print the array. Try doing this without looking at the book. + myList = [] +userList = input('Enter 5 numbers: ') +for items in userList: + myList.append(int(items)) +print(myList) + 15. Write a program that take an array like the following, and print the average. Use the len function, don't just use 15, because that won't work if the list size changes. @@ -105,4 +137,8 @@ my_list = [3,12,3,5,3,4,6,8,5,3,5,6,3,2,4] - + myList = [3,12,3,5,3,4,6,8,5,3,5,6,3,2,4] +ListSum = sum(myList) +ListLength = len(myList) +ListAverage = ListSum/ListLength +print(ListAverage) diff --git a/part_1.py b/part_1.py deleted file mode 100644 index 792d600..0000000 --- a/part_1.py +++ /dev/null @@ -1 +0,0 @@ -# diff --git a/part_2.py b/part_2.py deleted file mode 100644 index 792d600..0000000 --- a/part_2.py +++ /dev/null @@ -1 +0,0 @@ -# diff --git a/part_3.py b/part_3.py deleted file mode 100644 index 792d600..0000000 --- a/part_3.py +++ /dev/null @@ -1 +0,0 @@ -# diff --git a/part_4.py b/part_4.py deleted file mode 100644 index 792d600..0000000 --- a/part_4.py +++ /dev/null @@ -1 +0,0 @@ -#