diff --git a/Lab 01 - Calculator/lab_01_part_a.py b/Lab 01 - Calculator/lab_01_part_a.py index 792d600..2828ad0 100644 --- a/Lab 01 - Calculator/lab_01_part_a.py +++ b/Lab 01 - Calculator/lab_01_part_a.py @@ -1 +1,3 @@ -# +cd# +temperature_Fahrenheit = int(input("enter temperature in fahrenheit: ")) +print("the temperature in celsius is", (temperature_Fahrenheit-32)/1.8) diff --git a/Lab 01 - Calculator/lab_01_part_b.py b/Lab 01 - Calculator/lab_01_part_b.py index 792d600..dfb3e0c 100644 --- a/Lab 01 - Calculator/lab_01_part_b.py +++ b/Lab 01 - Calculator/lab_01_part_b.py @@ -1 +1,5 @@ -# + +T_Height = int(input("enter the height of the trapezoid: ")) +T_lengthbbase = int(input("Enter the length of the bottom base: ")) +T_lengthtbase = int(input("Enter the length of the top base: ")) +print("the area is: ",(T_lengthbbase + T_lengthtbase)*0.5*T_Height) diff --git a/Lab 01 - Calculator/lab_01_part_c.py b/Lab 01 - Calculator/lab_01_part_c.py index 792d600..61fbecf 100644 --- a/Lab 01 - Calculator/lab_01_part_c.py +++ b/Lab 01 - Calculator/lab_01_part_c.py @@ -1 +1,4 @@ # +x = int(input("number1: ")) +y = int(input("number2: ")) +print(x+y) diff --git a/Lab 03 - Create a Quiz/main_program.py b/Lab 03 - Create a Quiz/main_program.py index 792d600..82c9b78 100644 --- a/Lab 03 - Create a Quiz/main_program.py +++ b/Lab 03 - Create a Quiz/main_program.py @@ -1 +1,43 @@ +#!/usr/bin/env python3 +#quiz +#jacob poncher +#11/3/17 # +correct_answers = 0 +answer1 = int(input('what is 5+6? ')) +if answer1 == 11: + print('correct') + correct_answers = correct_answers + 1 +else: + print('incorrect') +print('what is 888 / 444? ') +print('a.1') +print('b.7') +print('c.3') +print('d.2') +answer2 = input('choice: ') +if answer2 == "d": + print("correct") + correct_answers = correct_answers + 1 +else: + print('incorrect') +print('we are learning java right now') +answer3 = input('true or false: ') +if answer3 == "false": + print('correct') + correct_answers = correct_answers + 1 +else: + print('incorrect') +answer4 = int(input("what is 2+11? ")) +if answer4 == 13: + print('correct') + correct_answers = correct_answers + 1 +else: + print('incorrect') +answer5 = int(input('what is 20/5? ')) +if answer5 == 4: + print('correct') + correct_answers = correct_answers + 1 +else: + print('incorrect') +print('you got',correct_answers*20,"%") diff --git a/Lab 04 - Camel/main_program.py b/Lab 04 - Camel/main_program.py index 792d600..35b1ef1 100644 --- a/Lab 04 - Camel/main_program.py +++ b/Lab 04 - Camel/main_program.py @@ -1 +1,80 @@ -# +#!/usr/bin/env python3 +# camel +#jacob poncher +# 11/8/17 + +""" Camel Game """ + +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!') +print('Survive your desert trek and out run the natives') +miles_traveled = 0 +thirst = 0 +camel_tiredness =0 +natives_distance =-20 +drinks = 5 +done = False +while not done: + print() + 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('choice: ') + print() + if choice == "q": + done = True + elif choice == "e": + print() + print("miles traveled: ",miles_traveled) + print("drinks in canteen: ", drinks) + print('the natives are',abs(natives_distance),"miles behind you.") + print() + elif choice == 'd': + camel_tiredness = 0 + print('the camel is happy') + natives_distance = natives_distance + random.randrange(7,14) + elif choice == "c": + miles_traveled = miles_traveled + random.randrange(10,21) + print('miles traveled:',miles_traveled) + thirst = thirst + 1 + camel_tiredness = camel_tiredness + random.randrange(1,4) + natives_distance = natives_distance + random.randrange(7,14) + elif choice == "b": + miles_traveled = miles_traveled + random.randrange(5,12) + print('miles traveled',miles_traveled) + thirst = thirst + 1 + camel_tiredness = camel_tiredness + 1 + natives_distance = natives_distance + random.randrange(7,14) + elif choice == "a": + if drinks > 0: + drinks = drinks - 1 + else: + print('error') + else: + print("That is not a valid choice! ") + + + if thirst > 4 and thirst < 6: + print('you are thirsty') + if thirst > 6: + print('you died of thirst') + done = True + + if natives_distance >= 0: + print('the natives have caught up') + done = True + elif natives_distance >= -15: + print('The natives are getting close') + + if miles_traveled >= 200 and done == False: + print('you win the game') + miles_travled = 0 + thirst = 0 + camel_tiredness = 0 + drinks = 5 + natives_distance = -20 diff --git a/Lab 06 - Loopy Lab/part_1.py b/Lab 06 - Loopy Lab/part_1.py index 8b13789..c2c76d2 100644 --- a/Lab 06 - Loopy Lab/part_1.py +++ b/Lab 06 - Loopy Lab/part_1.py @@ -1 +1,18 @@ +# !/usr/bin/env/ python3 +#part 1 +#jacob poncher +#11/15/17 +# start a variable at 10 +x = 10 + +#create a loop to do 9 rows +for i in range(1,10): + #create a loop to print digits + for j in range(i): + #print the variable + print(x, end=' ') + #increment the variable + x= x+1 + #print a 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..1b1104e 100644 --- a/Lab 06 - Loopy Lab/part_2.py +++ b/Lab 06 - Loopy Lab/part_2.py @@ -1 +1,12 @@ -# +#!/usr/bin/env/ python3 +#part 1 +#jacob poncher +#11/15/17 +# input statement for number +number = int(input('number: ')) +# print o times 2 for the input +print('o'* (number*2 + 2 )) +# loop to print 2 times the number of o +for i in range(number - 2): + print('o' + ' '*(number*2) + 'o') +print('o'* (number*2 + 2)) \ 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..60cb84d 100644 --- a/Lab 06 - Loopy Lab/part_4.py +++ b/Lab 06 - Loopy Lab/part_4.py @@ -1 +1,69 @@ -# +""" + 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 + +# 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) + + for y in range(0,500,10): + for x in range(0,700,10): + pygame.draw.rect(screen, GREEN ,[x, y, 5, 5], 0) + + print() + + + + # --- Drawing code should go here + + # --- 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() \ No newline at end of file diff --git a/Lab 07 - Adventure/main_program.py b/Lab 07 - Adventure/main_program.py index 792d600..b55b118 100644 --- a/Lab 07 - Adventure/main_program.py +++ b/Lab 07 - Adventure/main_program.py @@ -1 +1,68 @@ -# +#!/usr/bin/env +# main_program.py +# jacob poncher +# 11/20/17 + +''' first draft of a text adventure game ''' + +room_list = [] +room = [ "you are in the entrance.there is a passage south to the rest of the house ",None, None, 3, None] +room_list.append(room) +room = ["you are in the office, go west to the front hall ",None,None,None,10] +room_list.append(room) +room = [ " you are in the kitchen,go east to the front hall ", None,3 ,None,None] +room_list.append(room) +room = [ " you are in the west front hall,kitchen is to the west,living room is south ,and office is to the east,entrance is north", 0 ,10, 4, 2] +room_list.append(room) +room = [ " You are in the living room,go north to the front hall ",3 ,None ,None ,None] +room_list.append(room) +room = ["you are in the bedroom,the back hall is to the east",None,8,None,None] +room_list.append(room) +room = [ " you are in the bedroom, go west to the center hall",None,None,None,9] +room_list.append(room) +room = ["you are in the bathroom,the back hall is to the west" ,None,None,None,8] +room_list.append(room) +room = [ "you are in the back hall,bedroom is to the west ,center hall is north ,and bathroom is to the east", 9 , 7 ,None,5] +room_list.append(room) +room = [" you are in the center hall,north is the east front hall ,south is the back hall and the bedroom is east " , 10, 6, 8, None] +room_list.append(room) +room = [" you are in the east front hall, office is to the east,center hall is south,west front hall is west",None,1,9,3] +room_list.append(room) + +current_room = 0 +done = False +while done == False: + print(room_list[current_room][0]) + print() + next_room = current_room + user_choice = input("what do you want to do: ") + if user_choice == "south": + next_room = room_list[current_room][3] + if next_room == None: + print('you cant go that way') + else: + current_room = next_room + + if user_choice == "north": + next_room = room_list[current_room][1] + if next_room == None: + print('you cant go that way') + else: + current_room = next_room + + if user_choice == "east": + next_room = room_list[current_room][2] + if next_room == None: + print('you cant go that way') + else: + current_room = next_room + + if user_choice == "west": + next_room = room_list[current_room][4] + if next_room == None: + print('you cant go that way') + else: + current_room = next_room + + if user_choice == "quit": + done = True \ No newline at end of file diff --git a/Worksheets/worksheet_01.txt b/Worksheets/worksheet_01.txt index 728ad02..26f5cc9 100644 --- a/Worksheets/worksheet_01.txt +++ b/Worksheets/worksheet_01.txt @@ -6,22 +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('yourname') 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) 0.666666666666 + print(2 // 3) 1 + 2nd is floor division 4. Write a line of code that creates a variable called pi and sets it to an appropriate value. + pi = 23 5. Why does this code not work? A = 22 print(a) + different variable name 1 is lower case 6. All of the variable names below can be used. But which ONE of these is the better variable name to use? @@ -31,8 +34,9 @@ Area AREA area - area_of_rectangle + 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 @@ -59,17 +63,17 @@ 2x x2x total% - #left + #left answer 8. Why does this code not work? print(a) a = 45 - + value is assigned after the print statement 9. Explain the mistake in this code: pi = float(3.14) - + pi doesnt end 10. This program runs, but the code still could be better. Explain what is wrong with the code. @@ -78,67 +82,69 @@ pi = x area = pi * radius ** 2 print(area) - + variable can be called x 11. Explain the mistake in the following code: x = 4 y = 5 a = ((x) * (y)) print(a) - + parenthesis can be removed from x and y 12. Explain the mistake in the following code: x = 4 y = 5 a = 3(x + y) print(a) - + another set of parenthesis needs to be around a 13. Explain the mistake in the following code: radius = input(float("Enter the radius:")) - + no int 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 but the 1st is less space 15. What is a constant? - + a fixed variable 16. How are variable names for constants different than other variable names? - + they dont change 17. What is a single quote and what is a double quote? Give and label an example of both. - + '' "" 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("python \n program") 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) - + /3 has to be outside the parenthesis 21. What is an ``operator'' in Python? - + = + - * / 22. What does the following program print out? x = 3 x + 1 print(x) - + 3 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")) + print(value) diff --git a/Worksheets/worksheet_02.txt b/Worksheets/worksheet_02.txt index abe4cd7..179ddde 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.) - +101010101010011110011111111010101010101 2. Give an example of a decimal number. - +55555 3. Give an example of a hexadecimal number. - +3ADF 4. Convert the numbers 1, 10, 100, 1000, and 10000 from binary to decimal. - +1 2 4 8 16 5. What is a compiler? - +turns the source code into machine code 6. What is source code? - +program typed by the user 7. What is machine language? (Don't just say binary. That's not correct.) - +set of instructions executed directly by a computer's cpu 8. What is a first generation language? (Don't just say binary. That's not correct.) - +one and zeros 9. What is a second generation language? - +assembly langauge 10. What is a third generation language? (Explain, don't just give one example.) - +language used to program 11. What is an interpreter and how does it differ from a compiler? - + looks at the source code and change to 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 ruby javascript c# php objective-c sql c 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 c++ python indeed 14. What is the difference between the ``syntax'' and ``semantics'' of a language? - +syntax is how is written and semantics is how its spelled 15. Pick a piece of technology, other than a computer you use regularly. Briefly describe the hardware and software that run on it. - + my phone has ram and a storage and has linux as the os diff --git a/Worksheets/worksheet_03.txt b/Worksheets/worksheet_03.txt index bc85b84..756cd0f 100644 --- a/Worksheets/worksheet_03.txt +++ b/Worksheets/worksheet_03.txt @@ -10,14 +10,22 @@ print("It is hot outside.") else: print("It is not hot out.") - + second parenthensis 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('number: ')) +if number < 0: + print("negative") +elif number > 1: + print("positive") +else: + print('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) - + number1 = int(input('number: ')) +if number1 > -10 and number1 < 10: + print('Success') 4. This runs, but there is something wrong. What is it? (1 pt) user_input = input("A cherry is a: ") @@ -27,7 +35,7 @@ print("Correct!") else: print("Incorrect.") - + it wont print choices until you after the input statement 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. @@ -38,13 +46,13 @@ print("x is positive.") else: print("x is not positive.") - + x doesnt have a value assigned and the equal sign doesnt need to be there 6. What three things are wrong with the following code? (3 pts) x = input("Enter a number: ") if x = 3 print("You entered 3") - + needs an int ,colon at the end of line 2,line 2 needs 2 equal signs 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? ") @@ -52,13 +60,13 @@ print("Correct!") else print("Incorrect! It is Beaker.") - + there needs to be another equal sign,: after else , else doesnt need be indented ,a needs to be answer 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!") - + glad needs to have an x== in front of it 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. Clearly label your guess and the actual answer. @@ -78,7 +86,11 @@ print("Fizz") if z: print("Buzz") - + guess ,x = 5 ,y=6 z=5 ,fizz ,buzz actual answer + 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,7 +108,18 @@ print(x == 5 and y == 10) print(x == 5 and y == 5) print(x == 5 or y == 5) - + false + false + true + false + false + true + true + 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,6 +132,11 @@ print( (2 == 2) == "True" ) print( (2 == 2) == True ) print(3 < "3") + true + true + false + false + true 12. What things are wrong with this section of code? @@ -130,6 +158,6 @@ else if user_input = C: money = 50 - + no print statement diff --git a/Worksheets/worksheet_04.txt b/Worksheets/worksheet_04.txt index 341b2fa..88a9fa6 100644 --- a/Worksheets/worksheet_04.txt +++ b/Worksheets/worksheet_04.txt @@ -13,18 +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. +name = input('your name: ') +for i 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(0,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. - +x = 11 +while x > 0: + x = x-1 + print(x) +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.") @@ -34,13 +46,17 @@ x = input("Enter a number: ") total = total + i print("The total is:", x) - + total is not defined , x needs an int, 6. Write a program that prints a random integer from 1 to 10 (inclusive). - +import random +num = random.randrange(11) +print(num) 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 +num = random.randrange(1,11) +print(num) 8. Write a Python program that will: (3 pts) * Ask the user for seven numbers @@ -48,7 +64,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. - + x = 0 +pos = 0 +neg = 0 +zero = 0 +for i in range(7): + num = int(input('number: ')) + x = x + num + if num < 0: + pos = pos + 1 + elif num > 0: + neg = neg + 1 + else: + zero = zero + 1 +print(x) +print('positive:',pos) +print('negative:',neg) +print('zero:',zero) 9. Coin flip tosser: (4 pts) * Create a program that will print a random 0 or 1. @@ -56,14 +88,52 @@ 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. - + import random +tails = 0 +heads = 0 +for i in range(0,50): + flip = random.randrange(0,2) + if flip == 0: + tails = tails + 1 + else: + heads = heads + 1 +print("tails: ",str(tails)) +print("heads: ",str(heads)) 10. Write a program that plays rock, paper, scissors: (4 pts) * Create a program that randomly prints 0, 1, or 2. - * Expand the program so it randomly prints rock, paper, or scissors + * Expand the program so it randomly prints rock, paper, or scissors0,1 using if statements. Don't select from a list, as shown in the chapter. * 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. - + import random +print('0 is rock') +print('1 is paper') +print('2 is scissors') +userchoice = int(input('0,1,2: ')) +compchoice = random.randrange(0,3) +for i in range(1): + if userchoice == 0: + if compchoice == 0: + print('its a tie') + elif compchoice == 1: + print('you win') + else: + print('you lost') + + if userchoice == 1: + if compchoice == 0: + print('you win') + elif compchoice == 1: + print('its a tie') + else: + print('you lost') + if userchoice == 2: + if compchoice == 1: + print('You Win') + elif compchoice == 2: + print('its a tie') + else: + print('you lost') diff --git a/Worksheets/worksheet_06.txt b/Worksheets/worksheet_06.txt index 35e5f13..bf95455 100644 --- a/Worksheets/worksheet_06.txt +++ b/Worksheets/worksheet_06.txt @@ -17,21 +17,22 @@ while x < 10: print(x) x = x + 2 - + guess: 0,2,4,6,8, + result:0,2,4,6,8 2. What does this program print out? x = 1 while x < 64: print(x) x = x * 2 - + 1,2,4,8,16,32 3. Why is the and x >= 0 not needed? x = 0 while x < 10 and x >= 0: print(x) x = x + 2 - + because x will never be less than zero 4. What does this program print out? (0 pts) Explain. (1 pt) x = 5 @@ -40,22 +41,22 @@ if x == "1": print("Blast off!") x = x - 1 - + 5,4,3,2,1,0 because the if statement x is equal to a string not an integer 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) x = float(input("Enter a number greater than zero: ")) while x <= 0: - print("Too small. Enter a number greater than zero: ") + x = int(input("Too small. Enter a number greater than zero: ")) 6. Fix the following code: x = 10 - while x < 0: + while x > 0: print(x) - x - 1 + x = x - 1 print("Blast-off") @@ -66,7 +67,7 @@ for i in range(10): print(i) i += 1 - + the i = 0 because its already in the for loop 8. Explain why the values printed for x are so different. (2 pts) # Sample 1 @@ -84,5 +85,5 @@ for j in range(10): x += 1 print(x) - + the spacing for 1 says it has 2 for loops and sample 2 has a nested for loop diff --git a/Worksheets/worksheet_07.txt b/Worksheets/worksheet_07.txt index a87ff3f..72467c7 100644 --- a/Worksheets/worksheet_07.txt +++ b/Worksheets/worksheet_07.txt @@ -8,7 +8,10 @@ 1. List the four types of data we've covered, and give an example of each: - +string ,"string" +integer ,7 +floating point,3.5 +boolean,True or False 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. @@ -18,13 +21,13 @@ print(my_list[1]) print(my_list[4]) print(my_list[5]) - + out of range because 101 is 4 not 5 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] @@ -33,21 +36,26 @@ print(my_list1) my_list2[2] = 10 print(my_list2) - + 5,2,6,10,101 5. What does this code print out? my_list = [3 * 5] print(my_list) my_list = [3] * 5 print(my_list) - + 15 + 3,3,3,3,3 6. What does this code print out? my_list = [5] for i in range(5): my_list.append(i) print(my_list) - + 5,0 + 5,0,1 + 5,0,1,2 + 5,0,1,2,3 + 5,0,1,2,3,4 7. What does this code print out? print(len("Hi")) @@ -55,18 +63,27 @@ print(len("Hi") + len("there.")) print(len("2")) print(len(2)) - + 2,9,8,1 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) + s + i + m + p + s + o + n 10. What does this code print out? @@ -74,29 +91,46 @@ for i in range(3): 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: + the -1 spot is: . 13. What does this code print out? s = "0123456789" print(s[1]) print(s[:3]) print(s[3:]) - + 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. - +answer = 0 +usernum = [] +number1 = int(input("number: ")) +usernum.append(number1) +number2 = int(input("number: ")) +usernum.append(number2) +number3 = int(input("number: ")) +usernum.append(number3) +number4 = int(input("number: ")) +usernum.append(number4) +number5 = int(input("number: ")) +usernum.append(number5) +for i in range(len(usernum)) + answer += usernum[i] +print(answer) 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 +139,10 @@ my_list = [3,12,3,5,3,4,6,8,5,3,5,6,3,2,4] + answer = 0 +my_list = [3,12,3,5,3,4,6,8,5,3,5,6,3,2,4] +for i in range(len(my_list)): + answer += my_list[i] +answer = answer/len(my_list) +print(answer)