From f6f92c42019fa71f2de3d7c38386a527ca8783b9 Mon Sep 17 00:00:00 2001 From: asiar22 Date: Thu, 2 Nov 2017 09:06:33 -0500 Subject: [PATCH 1/3] Adding Chapter 1 Labs --- Lab 01 - Calculator/lab_01_part_a.py | 11 ++++++++++- Lab 01 - Calculator/lab_01_part_b.py | 14 +++++++++++++- Lab 01 - Calculator/lab_01_part_c.py | 12 +++++++++++- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/Lab 01 - Calculator/lab_01_part_a.py b/Lab 01 - Calculator/lab_01_part_a.py index 792d600..f7e6b44 100644 --- a/Lab 01 - Calculator/lab_01_part_a.py +++ b/Lab 01 - Calculator/lab_01_part_a.py @@ -1 +1,10 @@ -# +#!/usr/bin/env python +# 1.1 Part A +# Asia Robinson +# 11/2/2017 + +Fahrenheit = int(input("Enter a temperature in Fahrenheit: ")) + +Celsius = (Fahrenheit - 32) * 5.0/9.0 + +print("The Temperature in Celsius: ", Celsius) diff --git a/Lab 01 - Calculator/lab_01_part_b.py b/Lab 01 - Calculator/lab_01_part_b.py index 792d600..52fbd25 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 python +# 1.2 Part B +# Asia Robinson +# 11/2/2017 + +print("Area of a trapezoid") +trapazoid_height_int = int(input("Enter the height of the trapezoid: ")) +trapazoid_length_bottom_int = int(input("Enter the length of the bottom base: ")) +trapazoid_length_top_int = int(input("Enter the length of the top base: ")) + +trapazoid_area_int = ( .5 * (trapazoid_length_bottom_int + trapazoid_length_top_int) * trapazoid_height_int) + +print('The area is:', trapazoid_area_int) diff --git a/Lab 01 - Calculator/lab_01_part_c.py b/Lab 01 - Calculator/lab_01_part_c.py index 792d600..92719fa 100644 --- a/Lab 01 - Calculator/lab_01_part_c.py +++ b/Lab 01 - Calculator/lab_01_part_c.py @@ -1 +1,11 @@ -# +#!/usr/bin/env python +# 1.3 Part C +# Asia Robinson +# 11/2/2017 + +Celsius = int(input("Enter a temperature in Celsius: ")) + +Fahrenheit = (9.0/5.0 * Celsius + 32) + +print("The Temperature in Fahrenheit: ", Fahrenheit) + From 46d83b560d94073a3698fc19e5e4a1112eed8339 Mon Sep 17 00:00:00 2001 From: asiar22 Date: Mon, 6 Nov 2017 09:14:04 -0600 Subject: [PATCH 2/3] Completed Labs 1 & 3 and Completed Chapter 1 Worksheet --- Lab 03 - Create a Quiz/main_program.py | 71 ++++++++++++++++++++- Lab 04 - Camel/main_program.py | 85 +++++++++++++++++++++++++- Worksheets/worksheet_01.txt | 50 +++++++++------ 3 files changed, 184 insertions(+), 22 deletions(-) diff --git a/Lab 03 - Create a Quiz/main_program.py b/Lab 03 - Create a Quiz/main_program.py index 792d600..ff6284b 100644 --- a/Lab 03 - Create a Quiz/main_program.py +++ b/Lab 03 - Create a Quiz/main_program.py @@ -1 +1,70 @@ -# +#!/usr/bin/env python +# 3.1 Create a Quiz +# Asia Robinson +# 11/2/2017 + +print("Quiz Time!") +print() +quiz1 = input("1. What is 61,090 - 59,352 ? ") +correct = 0 +incorrect = 0 + +if quiz1 == ("1738"): + print("Ayee You're Right!") + correct = correct + 1 +else: + print("Incorrect") + incorrect = correct - 1 +print() +quiz2 = input("2. What is Fetty Wap government first name? ") +if quiz2 == ("willie"): + print("Ayee You're Right!") + correct = correct + 1 +else: + print("Incorrect") + incorrect = correct - 1 +print() +quiz3 = ("3. What is Asia favorite color?") +print(quiz3) +print("a. Red") +print("b. Blue") +print("c. Green") +answer = input("Your choice: ") +if answer == "b": + print("Ayee You're Right!") + correct = correct + 1 +else: + print ("Incorrect") + incorrect = correct - 1 +print() +quiz4 = ("4. Why is a bad joke like a bad pencil?") +print(quiz4) +print("a. because it has no point") +print("b. because it's not sharp") +answer = input("Your choice: ") +if answer == "a": + print("Ayee You're Right!") + correct = correct + 1 +else: + print ("Incorrect") + incorrect = correct - 1 +print() +quiz5 = ("5. Mary's father has 5 daughters. Nana, Nene, NiNi, and NoNo. Who's the 5th daughter ?") +print(quiz5) +print("a. Nicki") +print("b. NuNu") +print("c. Mary") +answer = input("Your choice: ") +if answer == "c": + print("Ayee You're Right!") + correct = correct + 1 +else: + print ("Incorrect") + incorrect = correct - 1 +print() +print ("Total score: " + str(correct) + "/5") +division = float(100)/float(5) +multiply = float(division*correct) +result = round(multiply) +print ("Total percentage is", int(result), "%") + diff --git a/Lab 04 - Camel/main_program.py b/Lab 04 - Camel/main_program.py index 792d600..9a5ed43 100644 --- a/Lab 04 - Camel/main_program.py +++ b/Lab 04 - Camel/main_program.py @@ -1 +1,84 @@ -# +#!/usr/bin/env python +# 4.2 Sample Run of Camel +# Asia Robinson +# 11/6/2017 + +import random +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 desert trek and out run the natives.') +print() +user_distance = 0 +camel = 0 +drinks = 3 +thirst = 0 +natives = -20 +done = False +while done == 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.') + choice = input('Your choice? ').upper() + if random.randint(1,20) == 5: + drinks = 3 + camel = 0 + print('You found an oasis!') + if (user_distance - natives) < 15: + print('The natives are getting close') + if natives >= user_distance: + print('The natives caught you and took you back to their village') + done = True + if camel >= 8: + print('Your camel is dead') + done = True + elif camel > 5: + print('Your camel is tired') + if thirst >= 6: + print('You died!') + done = True + elif thirst > 4: + print('You are thirsty!') + + if choice == 'Q': + done = True + + elif choice == 'E': + print('Miles traveled: ' + str(user_distance)) + print('Drinks in canteen: ' + str(drinks)) + print('The natives are ' + str(abs(user_distance-natives)) + ' miles behind you.') + + elif choice == 'D': + camel = 0 + print('The camel is happy') + natives = random.randint(7,14) + natives + + elif choice == 'C': + r = random.randint(10,20) + user_distance = r + user_distance + print('You traveled ' + str(r) + ' miles!') + thirst = thirst + 1 + camel = random.randint(1,3) + n = random.randint(7,14) + natives = n + natives + print('The natives have moved ' + str(n) + ' miles!') + + elif choice == 'B': + R = random.randint(5, 12) + Ran = random.randint(7,14) + user_distance = user_distance + R + print('You have traveled ' + str(R) + ' miles!') + thirst = thirst + 1 + camel = camel + 1 + natives = natives + Ran + print('The natives have moved ' + str(Ran) + ' miles!') + + elif choice == 'A': + if drinks > 0: + drinks = drinks - 1 + thirst = 0 + else: + print('You cannot drink from an empty canteen') + diff --git a/Worksheets/worksheet_01.txt b/Worksheets/worksheet_01.txt index 728ad02..103b0d4 100644 --- a/Worksheets/worksheet_01.txt +++ b/Worksheets/worksheet_01.txt @@ -6,25 +6,25 @@ and punctuation. Please limit the length of each line to 80 characters. 1. Write a line of code that will print your name. - +print("Asia Robinson") 2. How do you enter a comment in a program? - - 3. What do the following lines of code output? - ALSO: Why do they give a different answer? +#comment + 3. What do the following lines of code output? the answer to 2/3 + ALSO: Why do they give a different answer? - print(2 / 3) + 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. - - 5. Why does this code not work? +pi = 3.14 + 5. Why does this code not work?THe capitalization is wrong A = 22 print(a) 6. All of the variable names below can be used. But which ONE of these is - the better variable name to use? + the better variable name to use? area_of_rectangle a A @@ -37,6 +37,7 @@ 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.) + total% and #left arent allowed apple Apple @@ -61,17 +62,18 @@ total% #left - 8. Why does this code not work? + 8. Why does this code not work? The computer doesn't have anything to define. print(a) a = 45 - 9. Explain the mistake in this code: + 9. Explain the mistake in this code: The mistake is that it's already an float. pi = float(3.14) 10. This program runs, but the code still could be better. Explain what is wrong with the code. + The only thing is wrong is that you made an extra varibale(x). radius = float(input("Radius:")) x = 3.14 @@ -80,66 +82,74 @@ print(area) 11. Explain the mistake in the following code: - + The mistake is the extra two parenthesis x = 4 y = 5 a = ((x) * (y)) print(a) 12. Explain the mistake in the following code: - + The mistake says that 'int' object is not callable x = 4 y = 5 a = 3(x + y) print(a) 13. Explain the mistake in the following code: + The mistake is that it could not convert string to float: 'Enter the radius:' radius = input(float("Enter the radius:")) 14. Do all these print the same value? Which one is better to use and why? + yes they all print the same value. print(2 / 3 + 4) is better to use because it's easier for the programmer to see. print(2/3+4) print(2 / 3 + 4) print( 2 / 3+ 4 ) 15. What is a constant? +A constant is values that can't be altrered by the program. 16. How are variable names for constants different than other variable names? + Unlike any 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. +(Double Quote):"Asia" +(Single Quote):'Asia' 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.) - - 19. Can a Python program print text to the screen using single quotes instead - of double quotes? +print("I love pizza \"\n with a side of nachos.") + + 19. Can a Python program print text to the screen using single quotes instead of double quotes? Yes 20. Why does this code not calculate the average? - + It's too generic. There's not enough code for the computer to interpret what your trying to make it do. print(3 + 4 + 5 / 3) 21. What is an ``operator'' in Python? + An operator is 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")) From 09d105bfefb01139a2bcc31a6550e5a04d6cb547 Mon Sep 17 00:00:00 2001 From: asiar22 Date: Thu, 16 Nov 2017 08:53:34 -0600 Subject: [PATCH 3/3] "Completed Chapter 6" --- Lab 06 - Back to Looping.py | 42 ++++++++++++++++++++++++++ Lab 06 - Loopy Lab/part_1.py | 11 +++++++ Lab 06 - Loopy Lab/part_2.py | 11 ++++++- Lab 06 - Loopy Lab/part_4.py | 58 +++++++++++++++++++++++++++++++++++- Worksheets/worksheet_03.txt | 36 ++++++++++++++++++---- Worksheets/worksheet_04.txt | 18 +++++++++++ Worksheets/worksheet_06.txt | 30 +++++++++++++++---- 7 files changed, 192 insertions(+), 14 deletions(-) create mode 100644 Lab 06 - Back to Looping.py diff --git a/Lab 06 - Back to Looping.py b/Lab 06 - Back to Looping.py new file mode 100644 index 0000000..0db0fb5 --- /dev/null +++ b/Lab 06 - Back to Looping.py @@ -0,0 +1,42 @@ +''' +for i in range(10): + print("", end = "* ") +print() +for i in range(5): + print("", end = "* ") +print() +for i in range(20): + print("", end = "* ") + + +for i in range(10): + for row in range(10): + print("*", end = " ") + print() + +for i in range(10): + for j in range(i+1): + print(j, end = " ") + print() + +for i in range(10): + for space in range(i): + print(" ",end= " ") + for digit in range(10-i): + print(digit,end= " ") + print() + +for i in range(1, 10): + for digit in range(1, 10): + print(digit * i, end = " ") + print() +''' + +for i in range(10): + for j in range(10-i): + print (" ",end=" ") + for j in range(1,i+1): + print (j,end=" ") + for j in range(i-1,0,-1): + print (j,end=" ") + print() diff --git a/Lab 06 - Loopy Lab/part_1.py b/Lab 06 - Loopy Lab/part_1.py index 8b13789..2b066cf 100644 --- a/Lab 06 - Loopy Lab/part_1.py +++ b/Lab 06 - Loopy Lab/part_1.py @@ -1 +1,12 @@ +#!/usr/bin/env python +# 6.2 Part 1 +# Asia Robinson +# 11/16/2017 +startnumber = 10 + +for row in range(1,10): + for col in range(row): + print(startnumber,end=" ") + startnumber += 1 + print() diff --git a/Lab 06 - Loopy Lab/part_2.py b/Lab 06 - Loopy Lab/part_2.py index 792d600..81cba11 100644 --- a/Lab 06 - Loopy Lab/part_2.py +++ b/Lab 06 - Loopy Lab/part_2.py @@ -1 +1,10 @@ -# +#!/usr/bin/env python +# 6.2 Part 2 +# Asia Robinson +# 11/15/2017 + +number = int(input("enter number of o: ")) +for row in range(number): + for column in range(2*number): + print('o' if row in(0,number-1) or column in(0,(2*number)-1) else ' ', end=' ') + print() diff --git a/Lab 06 - Loopy Lab/part_4.py b/Lab 06 - Loopy Lab/part_4.py index 792d600..1622fd6 100644 --- a/Lab 06 - Loopy Lab/part_4.py +++ b/Lab 06 - Loopy Lab/part_4.py @@ -1 +1,57 @@ -# +#!/usr/bin/env python +# 6.2 Part 4 +# Asia Robinson +# 11/16/2017 + +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 + +# 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(WHITE) + + # --- Drawing code should go here + for square in range(1, 700, 10): + for y in range(1, 500, 10): + pygame.draw.rect(screen,RED,[x,y,7,7]) + # --- 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/Worksheets/worksheet_03.txt b/Worksheets/worksheet_03.txt index bc85b84..d817971 100644 --- a/Worksheets/worksheet_03.txt +++ b/Worksheets/worksheet_03.txt @@ -4,6 +4,7 @@ 1. What is missing from this code? (1 pt) + It's missing another parenthesis, so it should be float(input("Temperature: ")). temperature = float(input("Temperature: ") if temperature > 90: @@ -15,9 +16,21 @@ 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 positive.') + else: + 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 -10 < number < 10 + 1: + print('Success') + 4. This runs, but there is something wrong. What is it? (1 pt) user_input = input("A cherry is a: ") @@ -28,6 +41,8 @@ else: print("Incorrect.") + The program printed "Correct!" when the user puts in a lowercase a. + 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,14 +54,19 @@ else: print("x is not positive.") - 6. What three things are wrong with the following code? (3 pts) + The first thing wrong is that it don't allow it to run 'x == 4', it has to be + 'x = 4' because == doesn't define x as a variable. The second thing wrong + is that it don't allow the user to put in a number, so it won't print 'x is + not positive.'. + 6. What three things are wrong with the following code? (3 pts) + It needs a colon after 3. The second problem is that it needs another "=" next to the other one. The last problem is that print is indented more than it should be. x = input("Enter a number: ") if x = 3 print("You entered 3") 7. There are four things wrong with this code. Identify all four issues. (4 pts) - + The first problem is that its missing ":" after the word beaker. The second and third problem is the indentation for "print" and "else". The fourth problem is it didn't define "a" answer = input("What is the name of Dr. Bunsen Honeydew's assistant? ") if a = "Beaker": print("Correct!") @@ -54,12 +74,12 @@ print("Incorrect! It is Beaker.") 8. This program doesn't work correctly. What is wrong? (1 pt) - + The indentation on the print line is too many. Erase one or two spaces. 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. + 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 @@ -67,6 +87,8 @@ While you don't need to write an explanation, make sure you understand why the computer prints what it does. Don't get caught flat-footed when you need to know later. (2 pts) + Buzz + yes. x = 5 y = x == 6 @@ -81,7 +103,8 @@ 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) - + Only the ones that are true + It prints if it's True and False x = 5 y = 10 z = 10 @@ -99,6 +122,7 @@ 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) + true or false print("3" == "3") print(" 3" == "3") @@ -114,7 +138,7 @@ 12. What things are wrong with this section of code? The programmer wants to set the money variable according to the initial occupation the user selects. (1 pt) - + I personally changed "else if" to "elif" then I added "=" to the other "=" to form "==" print("Welcome to Oregon Trail!") print("A. Banker") diff --git a/Worksheets/worksheet_04.txt b/Worksheets/worksheet_04.txt index 341b2fa..51983da 100644 --- a/Worksheets/worksheet_04.txt +++ b/Worksheets/worksheet_04.txt @@ -14,18 +14,36 @@ 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. + for i in range(10): + print('Asia') + 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, 100 + 1, 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. + countdown = 10 + while countdown >= 0: + print(countdown) + countdown = countdown - 1 + print('Blast Off!') + 5. There are three things wrong with this program. List each. (3 pts) + The first problem is that the program prints out the last input instead of adding them all together and printing the total. The second problem is the 'total = total + i' line of code. The third problem is that we need to add 'int' in front of 'input("Enter a number: ")'. print("This program takes three numbers and returns the sum.") total = 0 diff --git a/Worksheets/worksheet_06.txt b/Worksheets/worksheet_06.txt index 35e5f13..5d91a0a 100644 --- a/Worksheets/worksheet_06.txt +++ b/Worksheets/worksheet_06.txt @@ -12,6 +12,8 @@ 1. What does this program print out? (Remember: TWO answers. Your guess and the actual result. Label both.) + 2 + 0,2,4,6,8 x = 0 while x < 10: @@ -19,21 +21,23 @@ x = x + 2 2. What does this program print out? - + 16 + 1,2,4,8,16,32 x = 1 while x < 64: print(x) x = x * 2 3. Why is the and x >= 0 not needed? - + we already defined 0 x = 0 while x < 10 and x >= 0: print(x) x = x + 2 4. What does this program print out? (0 pts) Explain. (1 pt) - + 5,4,3,2,1,0 + 5,4,3,2,1,0 x = 5 while x >= 0: print(x) @@ -48,7 +52,13 @@ while x <= 0: print("Too small. Enter a number greater than zero: ") - + + x = float(input("Enter a number greater than zero: ")) + + while x <= 0: + print("Too small. Enter a number greater than zero: ") + x = input("Enter a number greater than zero: ") + 6. Fix the following code: x = 10 @@ -59,16 +69,24 @@ print("Blast-off") + x = 10 + +while x > 0: + print(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) - + It has more code then it needs. You dont need the i += 1 because without it, it still works. The computer already know i = 0. i = 0 for i in range(10): print(i) i += 1 8. Explain why the values printed for x are so different. (2 pts) - + The indentation changes the code. Making the computer add on Sample 1 and multiply and more on sample 2. # Sample 1 x = 0 for i in range(10):