diff --git a/Lab 01 - Calculator/lab_01_part_a.py b/Lab 01 - Calculator/lab_01_part_a.py index 792d600..3e97c51 100644 --- a/Lab 01 - Calculator/lab_01_part_a.py +++ b/Lab 01 - Calculator/lab_01_part_a.py @@ -1 +1,4 @@ -# +# 1.1 Part A +Fahrenheit= int(input("Enter temperature in Fahrenheit: ")) +Celsius= ((Fahrenheit-32)*(5/9)) +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..16fb163 100644 --- a/Lab 01 - Calculator/lab_01_part_b.py +++ b/Lab 01 - Calculator/lab_01_part_b.py @@ -1 +1,7 @@ -# +# 1.2 Part B +print("Area of a trapezoid") +height= int(input("Enter the height of the trapezoid: ")) +lengthBottom= int(input("Enter the length of the bottom base: ")) +lengthTop= int(input("Enter the length of the top base: ")) +area= ((1/2)*(lengthBottom+lengthTop)*height) +print("The area is:",area) diff --git a/Lab 01 - Calculator/lab_01_part_c.py b/Lab 01 - Calculator/lab_01_part_c.py index 792d600..3a3fdc5 100644 --- a/Lab 01 - Calculator/lab_01_part_c.py +++ b/Lab 01 - Calculator/lab_01_part_c.py @@ -1 +1,5 @@ -# +# 1.3 Part C +print('For the volume of a sphere.') +radius= int(input('Enter the radius: ')) +volume= ((4/3)*(3.14*(radius**3))) +print('The volume is about', volume, '.') diff --git a/Lab 03 - Create a Quiz/main_program.py b/Lab 03 - Create a Quiz/main_program.py index 792d600..75fe05f 100644 --- a/Lab 03 - Create a Quiz/main_program.py +++ b/Lab 03 - Create a Quiz/main_program.py @@ -1 +1,53 @@ -# +print("Time for a Quiz!") +print("") +total= 0 + +question_one = int(input("1. What is my favorite number? ")) +if question_one == 8: + total= total+1 + print("Correct!") +else: + print("Incorrect.") +print("") + +question_two = input("2. What did Jack break when he fell down? ") +if question_two == "his crown": + print("Correct!") + total= total+1 +else: + print("Incorrect.") +print("") +print("3. What do your eyes do?") +answer_one= "1. smell" +answer_two= "2. see" +answer_three= "3. hear" +print(answer_one) +print(answer_two) +print(answer_three) +question_three= input("? ") +if question_three == "see": + print("Correct!") + total= total+1 +else: + print("Incorrect!") +print("") + +question_four= input("4. Is this the fourth question? ") +if question_four == "yes": + print("Correct!") + total= total+1 +else: + print("Incorrect.") +print("") + +question_five= input("What is the radius of the sun in miles? ") +if question_five== "432,288": + print("Correct!") + total= total+1 +else: + print("Incorrect.") +print("") + +print("Wow! You got", total, "questions right!") +print("That means your total score is", (100*(total/5)),"percent!") +print("") diff --git a/Lab 04 - Camel/main_program.py b/Lab 04 - Camel/main_program.py index 792d600..ec2890c 100644 --- a/Lab 04 - Camel/main_program.py +++ b/Lab 04 - Camel/main_program.py @@ -1 +1,98 @@ -# +# Completed Lab 4 +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("Hint: all capitals.") +print() + +A = "A. Drink from your canteen." +B = "B. Ahead moderate speed." +C = "C. Ahead full speed." +D = "D. Stop for the night." +E = "E. Status check." +Q = "Q. Quit." +done = False +miles_traveled = 0 +thirst = 0 +camel_tiredness = 0 +natives_distance = -20 +canteen_drinks = 5 + +while not done: + print(A) + print(B) + print(C) + print(D) + print(E) + print(Q) + user_choice= input("Your choice: ") + if user_choice == "Q": + done=True + + elif user_choice == "E": + print() + print("Miles traveled:", miles_traveled) + print("Drinks in canteen:", canteen_drinks) + print("The natives are", natives_distance, "miles behind you.") + print() + elif user_choice == "D": + camel_tiredness= 0 + natives_distance+= random.randrange(7,14) + print() + print("Your camel is fully rested.") + print() + elif user_choice == "C": + miles_traveled+= random.randrange(10,21) + thirst= thirst+1 + camel_tiredness+= random.randrange(1,4) + natives_distance+= random.randrange(7,14) + oasis= random.randrange(1,21) + print() + print("Your miles traveled are:", miles_traveled) + print() + elif user_choice == "B": + miles_traveled+= random.randrange(5,12) + thirst+= 1 + camel_tiredness+= 1 + natives_distance+= random.randrange(7,14) + print() + print("Your miles traveled are:", miles_traveled) + print() + elif user_choice == "A": + thirst= 0 + canteen_drinks-= 1 + print() + print("Your thrist is:", thirst) + print() + + if oasis == 20: + camel_tiredness= 0 + thirst= 0 + canteen += 3 + print("You found an oasis! You are no longer thirsty, you filled your canteen and your camel has rested.") + if miles_traveled >= 200 and not done: + print("You've won the game!") + done= True + if thirst > 4 and thirst <= 6 and not done: + print("You are thirsty!") + print() + if thirst > 6: + print("You died of thirst.") + print("You lose.") + print() + done=True + if camel_tiredness > 5 and camel_tiredness <= 8 and not done: + print("Your camel is getting tired.") + print() + if camel_tiredness > 8: + print("Your camel is dead.") + print("You lose.") + done= True + if natives_distance >= miles_traveled: + print("The natives have caught up to you.") + print("You lose.") + done= True + if natives_distance > miles_traveled+15: + print("The natives are getting close!") + print() diff --git a/Lab 06 - Loopy Lab/part_1.py b/Lab 06 - Loopy Lab/part_1.py index 8b13789..5733418 100644 --- a/Lab 06 - Loopy Lab/part_1.py +++ b/Lab 06 - Loopy Lab/part_1.py @@ -1 +1,7 @@ - +# Lab 1 chapter 6 +x = 10 +for i in range(9): + for number in range(i+1): + print(x, end=" ") + x+= 1 + print() diff --git a/Lab 06 - Loopy Lab/part_2.py b/Lab 06 - Loopy Lab/part_2.py index 792d600..b0614fc 100644 --- a/Lab 06 - Loopy Lab/part_2.py +++ b/Lab 06 - Loopy Lab/part_2.py @@ -1 +1,13 @@ -# +#Chapter 6 Lab 2 +number = int(input("Number = ")) +letter = "o" +spaces = " " * ((number * 2) - 3) +for row in range(number): + print(letter*2, end="") +print() +for row in range(number-2): + print(letter, spaces, end="") + print(letter) +for row in range(number): + print(letter*2, end="") +print() diff --git a/Lab 07 - Adventure/main_program.py b/Lab 07 - Adventure/main_program.py index 792d600..bee13f7 100644 --- a/Lab 07 - Adventure/main_program.py +++ b/Lab 07 - Adventure/main_program.py @@ -1 +1,42 @@ -# +#!/usr/bin/env +#Lab 7: Adventure +#Evie Nelson +#11/20/17 +''' An adventure game where the user can move through different rooms using north, east, south, and west. ''' +#First, sketch the layout of the dungeon +#All of the rooms: +roomZero = ["You are in the main hall. There is a room to the north, to the east, and to the west.", 3, 2, None, 1] +roomOne = ["You are in the ballroom. There is a room to the east.", None, 0, None, None] +roomTwo = ["You are in the study. There is a room to the north and to the west.", 4, None, None, 0] +roomThree = ["You are in the west hall. There is a room to the east, to the south , and to the west.", None, 4, 0, 7] +roomFour = ["You are in the east hall. There is a room to the north, to the east, to the south, and to the west.", 6, 5, 2, 3] +roomFive = ["You are in the weapon room. There is a room to the west.", None, None, None, 4] +roomSix = ["You are in the meeting room. There is a room to the south.", None, None, 4, None] +roomSeven = ["You are in the bedroom. There is a large tapestry to the north and a room to the east.", 8, 3, None, None] +roomEight = ["You are in the secret room. There is a room to the south.", None, None, 7, None] +roomList = [roomZero, roomOne, roomTwo, roomThree, roomFour, roomFive, roomSix, roomSeven, roomEight] +currentRoom= 0 +#print(roomList) +currentRoom= 0 +done = False +while done != True: + print() + print(roomList[currentRoom][0]) + userInput = input("What direction? ") +#directions + if userInput.lower()[0] == "n": + nextRoom = roomList[currentRoom][1] + elif userInput.lower()[0] == "e": + nextRoom = roomList[currentRoom][2] + elif userInput.lower()[0] == "s": + nextRoom = roomList[currentRoom][3] + elif userInput.lower()[0] == "w": + nextRoom = roomList[currentRoom][4] + elif userInput.lower()[0] == "q": + break +#if there isn't a room + if nextRoom == None: + print() + print("You can't go that way.") + else: + currentRoom = nextRoom diff --git a/Worksheets/worksheet_01.txt b/Worksheets/worksheet_01.txt index 728ad02..b093071 100644 --- a/Worksheets/worksheet_01.txt +++ b/Worksheets/worksheet_01.txt @@ -6,22 +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('Evie Nelson') 2. How do you enter a comment in a program? - + #comment 3. What do the following lines of code output? ALSO: Why do they give a different answer? - print(2 / 3) - print(2 // 3) + print(2 / 3) outputs 0.66666666 + print(2 // 3) outputs 0 + They output different answers since they are different equations. 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) + The varaible is a capital a, so the print should be print(A). 6. All of the variable names below can be used. But which ONE of these is the better variable name to use? @@ -33,6 +35,7 @@ area area_of_rectangle Area_Of_Rectangle + The last one, Area_Of_Rectangle, is the best to use since it is the most specific. 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 @@ -60,15 +63,18 @@ x2x total% #left + The variables #left, account number, account.number, great.big.variable, 2x, 1Apple, total%, and account# are not valid variable names in python. 8. Why does this code not work? print(a) a = 45 + The variable a needs to be before the print line. 9. Explain the mistake in this code: pi = float(3.14) + There is no print function. 10. This program runs, but the code still could be better. Explain what is wrong with the code. @@ -78,6 +84,7 @@ pi = x area = pi * radius ** 2 print(area) + The varaible pi could be set to 3.14 instead of x. 11. Explain the mistake in the following code: @@ -85,6 +92,7 @@ y = 5 a = ((x) * (y)) print(a) + The a = ((x) * (y)) could be changed to a = (x*y). 12. Explain the mistake in the following code: @@ -92,23 +100,29 @@ y = 5 a = 3(x + y) print(a) + The a = 3(x + y) should be a = (3*(x+y)). 13. Explain the mistake in the following code: radius = input(float("Enter the radius:")) + It should be radius = float(input("Enter the radius:")). 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 ) + They all work but the first one is best to use since it is the easiest to read and understand. 15. What is a constant? + A variable that is set to one value and therefore is always that value. 16. How are variable names for constants different than other variable names? + They never change. 17. What is a single quote and what is a double quote? Give and label an example of both. + A single quote is ' and a double quote is " . 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 @@ -116,30 +130,34 @@ 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? print(3 + 4 + 5 / 3) - + The 3, 4, and 5 added together need to be in () so they will be added together before being didveded by 3. 21. What is an ``operator'' in Python? + The symbols that allow math equations to be done in python. Such as <, >, +, -, *, **, and /. 22. What does the following program print out? x = 3 x + 1 print(x) - + It will print out 3. 23. Correct the following code: user_name = input("Enter your name: )" - + The correct code would be user_name = input("Enter your name: ") 24. Correct the following code: value = int(input(print("Enter your age"))) - + The code needs to be: + value = int(input("Enter your age")) + print(value) diff --git a/Worksheets/worksheet_02.txt b/Worksheets/worksheet_02.txt index abe4cd7..e42c602 100644 --- a/Worksheets/worksheet_02.txt +++ b/Worksheets/worksheet_02.txt @@ -6,37 +6,52 @@ 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.) + The decimal of 10 has a binary of 1010 and a hexadecimal of A. 2. Give an example of a decimal number. + 5 3. Give an example of a hexadecimal number. + 11 4. Convert the numbers 1, 10, 100, 1000, and 10000 from binary to decimal. + The decimals would be 1, 2, 4, 8, and 16. - 5. What is a compiler? + 5. What is a compiler? + A compiler changes the program entered by the user into machine code. 6. What is source code? + The code typed in by the user. 7. What is machine language? (Don't just say binary. That's not correct.) + Machine language and machine code are the only languages computers can understand, they use numbers to represent commands. 8. What is a first generation language? (Don't just say binary. That's not correct.) + The first programming language, it is the same thing as the machine's native language. 9. What is a second generation language? + The second generation language is an assembly language that uses a mnemonic, that is changed with a compiler to the machine's native language. 10. What is a third generation language? (Explain, don't just give one example.) + The third generation language is a type of source code typed in by the user that is changed into machine code by the compiler. 11. What is an interpreter and how does it differ from a compiler? + An interpreter translates the source code into machine code for any type of computer, while a compiler only works for some types. 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, Ruby, and JavaScript are the five most popular programming languages according to inc.com. 13. Look at the job boards and see what languages people are looking for. List the languages and the job board you looked at. + 14. What is the difference between the ``syntax'' and ``semantics'' of a language? + Syntax is the structure or grammar of a language, while semantics is the meaning behind the language. 15. Pick a piece of technology, other than a computer you use regularly. Briefly describe the hardware and software that run on it. + diff --git a/Worksheets/worksheet_03.txt b/Worksheets/worksheet_03.txt index bc85b84..539c66b 100644 --- a/Worksheets/worksheet_03.txt +++ b/Worksheets/worksheet_03.txt @@ -10,13 +10,26 @@ print("It is hot outside.") else: print("It is not hot out.") + The first line should read temperature = float(input("Temperature: ")), ending with two ). 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("The number is positive.") + elif number < 0: + print("The number is negitive.") + elif number == 0: + print("The 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 number > -10 < 10: + print("Success") + else: + print("") 4. This runs, but there is something wrong. What is it? (1 pt) @@ -27,6 +40,9 @@ print("Correct!") else: print("Incorrect.") + The two print statements after the user_input line will only print + after the user hits enter. The print statements in line one and two + should be put before the user_input line. 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. @@ -38,12 +54,15 @@ print("x is positive.") else: print("x is not positive.") + The x variable should only have one equal sign and 6. What three things are wrong with the following code? (3 pts) x = input("Enter a number: ") if x = 3 print("You entered 3") + The x = input( should be x = int(input( , the secind line needs to + be if x = 3: , and there is no else: clause. 7. There are four things wrong with this code. Identify all four issues. (4 pts) @@ -52,12 +71,15 @@ print("Correct!") else print("Incorrect! It is Beaker.") + The second line needs two equal signs, in the forth line else needs + to be else: and needs to be inline with the if line. 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!") + It needs an else: line incase the input isn't Happy or Glad. 9. Look at the code below. Write you best guess here on what it will print. Next, run the code and see if you are correct. @@ -78,6 +100,18 @@ print("Fizz") if z: print("Buzz") + I guess the program will print: + x= 5 + y= 6 + z= 5 + Buzz + Fizz + Buzz + When tested, the program outputs: + 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) @@ -96,6 +130,29 @@ print(x == 5 and y == 10) print(x == 5 and y == 5) print(x == 5 or y == 5) + I guess the program will output: + True + False + True + True + False + False + False + True + False + True + When tested, the program outputs: + 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) @@ -109,8 +166,27 @@ print( (2 == 2) == "True" ) print( (2 == 2) == True ) print(3 < "3") + I guess the program will output: + True + False + True + True + False + False + False + True + False + When tested, the program outputs: + True + False + True + True + True + False + False + True + Error - 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) @@ -129,7 +205,26 @@ money = 70 else if user_input = C: money = 50 + + The program should look like: + print("Welcome to Oregon Trail!") + + A = "A. Banker" + B = "B. Carpente" + C = "C. Farmer" + + print(A) + print(B) + print(C) + + user_input = input("What is your occupation?") + if user_input == A: + money = 100 + elif user_input == B: + money = 70 + elif user_input == C: + money = 50 diff --git a/Worksheets/worksheet_04.txt b/Worksheets/worksheet_04.txt index 341b2fa..9a2c8f3 100644 --- a/Worksheets/worksheet_04.txt +++ b/Worksheets/worksheet_04.txt @@ -13,17 +13,30 @@ 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("Evie Nelson") + 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(10): + 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, 102, 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. + i = 10 + while i > 0: + print(i) + i= i-1 + print("Blast off!") 5. There are three things wrong with this program. List each. (3 pts) @@ -34,12 +47,21 @@ x = input("Enter a number: ") total = total + i print("The total is:", x) + The x variable should be an i, the input statment should have an int + function before it, and in the print statement it should be the total + variable, not the x variable. 6. Write a program that prints a random integer from 1 to 10 (inclusive). + import random + my_number = random.randrange(1, 11) + print(my_number) 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 + my_number = random.random()*10+1 + print(my_number) 8. Write a Python program that will: (3 pts) @@ -48,6 +70,23 @@ * Print the count of the positive entries, the number entries equal to zero, and the number of negative entries. Use an if, elif, else chain, not just three if statements. + total= 0 + posnumber= 0 + zero= 0 + negnumber= 0 + for i in range(7): + usernumber= int(input("Enter a number: ")) + total+= usernumber + if usernumber > 0: + posnumber+= 1 + elif usernumber== 0: + zero+= 1 + elif usernumber < 0: + negnumber+= 1 + print("The sum of all the numbers entered is:", total) + print("The number of positive entries is:", posnumber) + print("The number of negitive entries is:", negnumber) + print("The number of entries that were the number zero is:", zero) 9. Coin flip tosser: (4 pts) @@ -56,6 +95,19 @@ using if statements. Don't select from a list, as shown in the chapter. * 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. + totalHeads= 0 + totalTails= 0 + for i in range(50): + import random + coinflip= random.randrange(0,2) + if coinflip== 1: + print("heads") + totalHeads= totalHeads+1 + elif coinflip== 0: + print("tails") + totalTails= totalTails+1 + print("Total number of heads:", totalHeads) + print("Total number of tails:", totalTails) 10. Write a program that plays rock, paper, scissors: (4 pts) @@ -65,5 +117,30 @@ * Add to the program so it first asks the user their choice. * (It will be easier if you have them enter 1, 2, or 3.) * Add conditional statement to figure out who wins. +print("I want to play rock, paper, scissors.") +useroutcome= input("What do you pick? ") +import random +outcome= random.randrange(0,3) +if useroutcome== "rock": + if outcome== 0: + print("Draw") + elif outcome== 1: + print("You lose.") + elif outcome== 2: + print("You win!") +elif useroutcome== "paper": + if outcome== 0: + print("You win!") + elif outcome== 1: + print("Draw") + elif outcome== 2: + print("You lose.") +elif useroutcome== "scissors": + if outcome== 0: + print("You lose.") + elif outcome== 1: + print("You win!") + elif outcome== 2: + print("Draw") diff --git a/Worksheets/worksheet_05.txt b/Worksheets/worksheet_05.txt index 275a18a..f84e03f 100644 --- a/Worksheets/worksheet_05.txt +++ b/Worksheets/worksheet_05.txt @@ -7,21 +7,32 @@ 1. Explain how the computer coordinate system differs from the standard Cartesian coordinate system. There are two main differences. List both. + The origin is in the top-right of the screen and as the y values increase + the coordinates move down. 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 + pygame.init() 3. Explain how WHITE = (255, 255, 255) represents a color. - + The three 255 represtent the amount of red, green, and blue. Having all of the colors + at full intensity means the color is white. + 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.) + Variables in all upper-case are constants, meaning they don't change. If the + color is meant to change, the variable name should be in lower-case. 5. What does the pygame.display.set_mode() function do? + It will open a display window to print the picture created by the code. 6. What does this for event in pygame.event.get() loop do? + It will allow the user to interact with the display window. 7. What is pygame.time.Clock used for? + It controls how fast the screen updates. 8. For this line of code: (3 pts) @@ -34,6 +45,7 @@ 9. What is the best way to repeat something over and over in a drawing? + 10. When drawing a rectangle, what happens if the specified line width is zero? diff --git a/Worksheets/worksheet_06.txt b/Worksheets/worksheet_06.txt index 35e5f13..1902f4e 100644 --- a/Worksheets/worksheet_06.txt +++ b/Worksheets/worksheet_06.txt @@ -17,6 +17,8 @@ while x < 10: print(x) x = x + 2 + My guess: 0,2,4,6,8 on seperate lines + Output: 0,2,4,6,8 on seperate lines 2. What does this program print out? @@ -24,6 +26,8 @@ while x < 64: print(x) x = x * 2 + My guess: 1,2,4,8,16,32 on seperate lines + Output: 1,2,4,8,16,32 om seperate lines 3. Why is the and x >= 0 not needed? @@ -31,6 +35,7 @@ while x < 10 and x >= 0: print(x) x = x + 2 + You need to check for things that can never be false, ie x=0. 4. What does this program print out? (0 pts) Explain. (1 pt) @@ -40,6 +45,8 @@ if x == "1": print("Blast off!") x = x - 1 + This will print a countdown from 5 to 0, but will not print blastoff. + The print("Blast off!") will not print because it is the wrong part of the code. 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) @@ -48,6 +55,12 @@ while x <= 0: print("Too small. Enter a number greater than zero: ") + + The program should look like: +x = float(input("Enter a number greater than zero: ")) + +while x <= 0: + x = float(input("Too small. Enter a number greater than zero: ")) 6. Fix the following code: @@ -59,6 +72,16 @@ print("Blast-off") + If the code is trying to print 1-10 backwards before printing Blast-off, + the program should look like this: +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) @@ -66,6 +89,9 @@ for i in range(10): print(i) i += 1 + The i = 0 part of the code is unneeded since the range function will + always start at 0. The i += 1 is also unnecessary since the code will + go by 1's until finished without it. 8. Explain why the values printed for x are so different. (2 pts) @@ -85,4 +111,6 @@ x += 1 print(x) - + For sample 1 the output is 20 since x is 10 plus 10, while for + sample 2 the code is in a loop so ten is added together 10 times, + and after looped 10 times the outside code adds 10. diff --git a/Worksheets/worksheet_07.txt b/Worksheets/worksheet_07.txt index a87ff3f..7e7b5ea 100644 --- a/Worksheets/worksheet_07.txt +++ b/Worksheets/worksheet_07.txt @@ -8,6 +8,7 @@ 1. List the four types of data we've covered, and give an example of each: + The four types of data are int (3), bool (True), str ("string"), and float (2.76). 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, @@ -18,12 +19,22 @@ print(my_list[1]) print(my_list[4]) print(my_list[5]) + This code will print out: + 2 + 101 + error 3. What does this code print out? my_list=[5, 2, 6, 8, 101] for my_item in my_list: print(my_item) + This code will print out: + 5 + 2 + 6 + 8 + 101 4. What does this code print out? @@ -33,6 +44,9 @@ print(my_list1) my_list2[2] = 10 print(my_list2) + The code will print: + [5, 2, 6, 8, 101] + error 5. What does this code print out? @@ -40,6 +54,9 @@ print(my_list) my_list = [3] * 5 print(my_list) + This code will print out: + [15] + [3, 3, 3, 3, 3] 6. What does this code print out? @@ -47,6 +64,8 @@ for i in range(5): my_list.append(i) print(my_list) + This program will output: + [5, 0, 1, 2, 3, 4] 7. What does this code print out? @@ -55,18 +74,35 @@ print(len("Hi") + len("there.")) print(len("2")) print(len(2)) + This code will output: + 2 + 9 + 8 + 1 + error 8. What does this code print out? print("Simpson" + "College") print("Simpson" + "College"[1]) print( ("Simpson" + "College")[1] ) + SimpsonCollege + Simpsono + i 9. What does this code print out? word = "Simpson" for letter in word: print(letter) + This code will print out: + S + i + m + p + s + o + n 10. What does this code print out? @@ -74,17 +110,24 @@ for i in range(3): word += "College" print(word) + This program will output: + SimpsonCollegeCollegeCollege 11. What does this code print out? word = "Hi" * 3 print(word) + This code will print out: + 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]) + This code will print out: + The 3rd spot is: + The -1 spot is: . 13. What does this code print out? @@ -92,10 +135,20 @@ print(s[1]) print(s[:3]) print(s[3:]) + This code will print out: + 1 + 012 + 3456789 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. + +userList = [] +for number in range(5): + number = int(input("Enter a number for a list: ")) + userList.append(number) +print(userList) 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 @@ -105,4 +158,10 @@ my_list = [3,12,3,5,3,4,6,8,5,3,5,6,3,2,4] +my_list = [3, 12, 3, 5, 3, 4] +list_total= 0 +for i in range(len(my_list)): + list_total += my_list[i] +list_total = list_total/len(my_list) +print(list_total)