├── 'X'MarkstheSpot.py ├── 1Through10,Part2.py ├── 2Through20Even.py ├── 2dListsandSlices.py ├── 4Columns.py ├── 4Columns2.0.py ├── AListIsLikeaMutableTuple.py ├── AListofLists.py ├── ATupleIsaSequence.py ├── ATupleisHeterogenous.py ├── Add,Subtract,orMultiply.py ├── AddComments!.py ├── AddOne.py ├── AddParentheses.py ├── AddingtoaValue.py ├── Administrators,Teachers,andStudents.py ├── Age.py ├── AgeGroup.py ├── AreaofaSquarewithDefaultParameters.py ├── Asterisk.py ├── AverageTestScore,Part2.py ├── AverageTestScore.py ├── BasicUserInput.py ├── BeadedBracelet.py ├── BikeFrameSize.py ├── BlackandWhiteSquares.py ├── BlockPyramid.py ├── BooleanVariables.py ├── Break.py ├── BubbleWrap.py ├── BubbleWrap2.0.py ├── Categories.py ├── Caterpillar.py ├── CharactersinaString.py ├── Checkerboard,v1.py ├── Checkerboard,v2.py ├── Checkerboard,v3.py ├── Checkerboard.py ├── CirclePyramid.py ├── CirclePyramid2.0.py ├── CirclePyramidwithComments.py ├── CircleSquareTriangle.py ├── CircleinaSquare.py ├── Citation.py ├── ColorCodedIncreasingLength.py ├── ColoredDartboard.py ├── ColorfulBracelet.py ├── ColorfulCaterpillar.py ├── CommentedProgram.py ├── ComparisonOperators.py ├── ConcatenatingTuples.py ├── ConcentricCircles.py ├── ContainsaLetter.py ├── ContainsaSubstring.py ├── ContainsaVowel.py ├── Continue.py ├── CookoutOrders.py ├── CoordinatePair.py ├── CoordinatePairs.py ├── CorrectPortion.py ├── CountOccurrences.py ├── Counting10to100byTens.py ├── Dartboard.py ├── Dartboardusingi.py ├── DashedLine.py ├── DefaultParameterValues.py ├── DigitsofPi.py ├── Divisibility,Part2.py ├── Divisibility.py ├── Divisibleby3.py ├── Doghouse.py ├── DottedLine.py ├── DoubleForLoop.py ├── EnterNameandAge.py ├── EnteraNumber.py ├── EnteraPositiveNumber.py ├── Enthusiasm.py ├── Exclamat!onPo!nts.py ├── ExtendedGreeting.py ├── FindtheError.py ├── FirstCharacter.py ├── FiveNumbers.py ├── FixThisProgram (1).py ├── FixThisProgram! (1).py ├── FixThisProgram!.py ├── FixThisProgram.py ├── FixThisTuple.py ├── ForLoop+WhileLoop.py ├── ForLoopsandLists,Part2.py ├── ForLoopsandLists.py ├── FourCircles.py ├── FourCircleswithComments.py ├── FourColoredTriangles.py ├── FourCorners.py ├── FruitsandVegetables.py ├── FullName&Citation.py ├── FunctionCallsAsParameters.py ├── FunctionsCallingFunctions.py ├── FunctionsandVariables,Part2.py ├── FunctionsandVariables.py ├── Geometry.py ├── Geometry2.0.py ├── Greeting.py ├── Grid.py ├── GroupsofCharacters.py ├── GuessMyNumber.py ├── GuessaNumber.py ├── GuessaNumber2.0.py ├── HappyFace.py ├── Hello,world!.py ├── Hello.py ├── Hexagon.py ├── HighJump.py ├── HowManyNames? (1).py ├── HowManyNames?.py ├── If-ElseStatement.py ├── IncreasingCircles.py ├── IncreasingLength.py ├── IncreasingSquares.py ├── Initials.py ├── IntroduceYourself,Part2.py ├── IntroduceYourself.py ├── IsItRaining?.py ├── KeysandValues.py ├── Kid'sShapesToy.py ├── LICENSE ├── LastNames.py ├── LengthofUser'sName.py ├── LettersinName.py ├── Librarian,Part2.py ├── Librarian.py ├── LineofIncreasingBlocks.py ├── ListedGreeting.py ├── ListofBooleans.py ├── ListofConsecutiveNumbers.py ├── ListofSquares.py ├── ListofTuples,TuplesofLists.py ├── LogicalOperators.py ├── LoopandaHalf.py ├── Lower.py ├── Lower2.0.py ├── MakeSomeVariables!.py ├── MaxInList.py ├── NameandAge.py ├── Negate.py ├── OldEnoughtoVote?.py ├── OperatorPrecedence.py ├── OperatorsandFloats.py ├── OperatorsandIntegers.py ├── OperatorsandStrings.py ├── Owls,Part2.py ├── Owls.py ├── Packing.py ├── Part1,RemoveAllFromString.py ├── Part1,ReplaceaLetter.py ├── Part2,ReplaceaLetter.py ├── PhoneBook.py ├── PhoneSignal.py ├── Plants.py ├── Positive,Negative,Zero.py ├── Positive,Zero,orNegative?.py ├── PresidentialEligibility-Extended.py ├── PresidentialEligibility.py ├── Print10Numbers.py ├── PrintMultipleTimes.py ├── PrintProduct.py ├── PrintaNumber.py ├── PrintingMultipleLines.py ├── ProgramTracing,Part2.py ├── ProgramTracing.py ├── README.md ├── RainbowOctagon.py ├── Rating.py ├── Recipe.py ├── Rectangle (1).py ├── Rectangle,Part2.py ├── Rectangle,Part3.py ├── Rectangle.py ├── Return10.py ├── RollerCoaster.py ├── RollingDice.py ├── RoomNames.py ├── RoundingError.py ├── RowofCircles.py ├── RunningTotal,Part2.py ├── RunningTotal.py ├── SadFace.py ├── ShapeStack.py ├── ShorterDashedLine.py ├── Sidewalk.py ├── Slinky.py ├── Slopes.py ├── Snowman.py ├── SpellingBee.py ├── SplittingaString.py ├── Square.py ├── SquareSwirl.py ├── SquareUsingLoops.py ├── StretchedSlinky.py ├── String<-->List.py ├── StringCollage.py ├── StringForLoopwithCharacters.py ├── StringForLoopwithIndices.py ├── StringImmutability.py ├── StringsToIntegers.py ├── SumTwoNumbers.py ├── Swapping.py ├── TableReservation.py ├── TeamSizes.py ├── TemperatureConverter,Part2.py ├── TemperatureConverter.py ├── TheinKeyword.py ├── ThelenFunction.py ├── TicTacToe.py ├── Transaction.py ├── Trapezoid.py ├── TuplesWithaSingleElement.py ├── TypeConversion,Part2.py ├── TypeConversion.py ├── UndefinedVariables.py ├── Unpacking.py ├── UnpackingParameters.py ├── VariableNaming.py ├── VariableTypes.py ├── VariablesinAction.py ├── Verticalname.py ├── VowelsinFirstName.py ├── Weather.py ├── WordLadder.py ├── XandYAxes.py ├── XandYAxeswithHashMarks.py ├── XandYAxiswithBoldedMarks.py ├── YourFirstIfStatement.py ├── appendandextend.py ├── count.py ├── find.py ├── remove.py ├── reverse.py ├── sort.py ├── strip.py ├── swapcase.py └── upperandlower.py /'X'MarkstheSpot.py: -------------------------------------------------------------------------------- 1 | # increase the speed of the turtle (I hate waiting) 2 | speed(5) 3 | 4 | #setup 5 | left(45) 6 | 7 | for x in range(4): 8 | ''' 9 | # the toatal length of the X must be 200 pixels 10 | # so the length of each prong must be 200 / 2 11 | # because there is two prongs that make up the 12 | # diamitor of the x 13 | ''' 14 | 15 | forward(200 / 2) 16 | backward(200 / 2) 17 | 18 | # left 90 to make next prong 19 | left(90) -------------------------------------------------------------------------------- /1Through10,Part2.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program prints the numbers 1 though 10. 3 | """ 4 | 5 | for i in range(1, 11): 6 | print i -------------------------------------------------------------------------------- /2Through20Even.py: -------------------------------------------------------------------------------- 1 | number = 0 2 | 3 | while(number < 20): 4 | number = number + 2 5 | print number -------------------------------------------------------------------------------- /2dListsandSlices.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program shows how slicing and the append method can be used in a 2d list. 3 | """ 4 | 5 | my_list = [] 6 | 7 | # Append 3 lists to my_list. 8 | for i in range(3): 9 | my_list.append([i * 1, i * 2, i * 4]) 10 | 11 | print my_list 12 | 13 | # Take a slice of the outer list. 14 | print my_list[0:2] 15 | 16 | # Take a slice of an inner list. 17 | print my_list[0][0:2] -------------------------------------------------------------------------------- /4Columns.py: -------------------------------------------------------------------------------- 1 | #move to the starting position 2 | penup() 3 | backward(100) 4 | left(90) 5 | backward(200) 6 | 7 | #draw first line 8 | pendown() 9 | forward(400) 10 | penup() 11 | 12 | # Move into position for middle line 13 | right(90) 14 | forward(100) 15 | right(90) 16 | 17 | # Draw second line 18 | pendown() 19 | forward(400) 20 | penup() 21 | 22 | # Move into position for last line 23 | left(90) 24 | forward(100) 25 | left(90) 26 | 27 | # Draw third line 28 | pendown() 29 | forward(400) -------------------------------------------------------------------------------- /4Columns2.0.py: -------------------------------------------------------------------------------- 1 | #move to the start pos 2 | penup() 3 | backward(100) 4 | left(90) 5 | backward(200) 6 | 7 | #execut4e this 8 | 9 | for x in range(3): 10 | pendown() 11 | forward(400) 12 | penup() 13 | backward(400) 14 | right(90) 15 | forward(100) 16 | left(90) -------------------------------------------------------------------------------- /AListIsLikeaMutableTuple.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program shows how indexing and slicing can be used with lists. 3 | """ 4 | 5 | # Creating an empty list: 6 | my_list = [] 7 | print my_list 8 | 9 | # Creating a list with things in it: 10 | list_of_things = ["hi", 3, 4.8] 11 | 12 | thing_zero = list_of_things[0] 13 | thing_one = list_of_things[1] 14 | thing_two = list_of_things[2] 15 | 16 | 17 | print thing_zero 18 | print thing_one 19 | print thing_two 20 | 21 | print list_of_things 22 | 23 | print len(list_of_things) 24 | 25 | # Unlike with a tuple, you can change a particular element in a list! 26 | list_of_things[0] = 2 27 | 28 | print list_of_things 29 | 30 | # Get everything starting at thing 0 and going up to BUT NOT INCLUDING thing 2 31 | print list_of_things[0:2] 32 | 33 | # This gets things 1 and 2 34 | print list_of_things[1:3] 35 | 36 | # This gets everything from thing 1 37 | # to the end. 38 | print list_of_things[1:] 39 | 40 | # This gets everything from the beginning up to but not including 41 | # thing 2 42 | print list_of_things[:2] 43 | 44 | # This gets the last thing. 45 | print list_of_things[-1] 46 | 47 | # This gets the last two things. 48 | print list_of_things[-2:] 49 | 50 | # This gets everything but the last thing. 51 | print list_of_things[:-1] -------------------------------------------------------------------------------- /AListofLists.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program shows how it is possible to index a list inside a list. 3 | """ 4 | 5 | my_list = [] 6 | my_list.append([1, 2, 3]) 7 | my_list.append([4, 5, 6]) 8 | 9 | print my_list 10 | 11 | # Indexing into myList gives you a list of numbers 12 | print my_list[0] 13 | 14 | # Indexing into THAT gives you a number 15 | print my_list[0][1] -------------------------------------------------------------------------------- /ATupleIsaSequence.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program shows the different ways to index a tuple. 3 | """ 4 | 5 | my_tuple = (1, 2, 3, 4, 5) 6 | 7 | print my_tuple[0] 8 | 9 | print my_tuple[:2] 10 | 11 | print len(my_tuple) 12 | 13 | # -- THE LINE BELOW WOULD CAUSE AN ERROR -- 14 | # my_tuple[0] = 10 -------------------------------------------------------------------------------- /ATupleisHeterogenous.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program shows the different elements that can be found within a tuple. 3 | """ 4 | 5 | my_tuple = (0, 1, "hi", (2, 3)) 6 | 7 | print my_tuple[0] 8 | print my_tuple[1] 9 | print my_tuple[2] 10 | 11 | # my_tuple[3] is, itself, a tuple. 12 | print my_tuple[3] 13 | 14 | # The first square bracket accesses the tuple (2, 3) within the larger tuple. 15 | # The second square bracket accesses the integers within this smaller tuple. 16 | print my_tuple[3][0] 17 | print my_tuple[3][1] -------------------------------------------------------------------------------- /Add,Subtract,orMultiply.py: -------------------------------------------------------------------------------- 1 | num1 = int(input("Please enter a number: ")) 2 | num2 = int(input("Please enter a number: ")) 3 | 4 | def Add(): 5 | answer = num1 + num2 6 | print "\n" + str(num1) + " + " + str(num2) + " = " + str(answer) 7 | def Sub(): 8 | answer = num1 - num2 9 | print "\n" + str(num1) + " - " + str(num2) + " = " + str(answer) 10 | def Mult(): 11 | answer = num1 * num2 12 | print "\n" + str(num1) + " * " + str(num2) + " = " + str(answer) 13 | 14 | ops = input("Please select an operation (add, subtract, multiply): ") 15 | 16 | if(ops != "add" and ops != "subtract" and ops != "multiply"): 17 | print("\nInvalid operation!") 18 | else: 19 | if(ops == "add"): 20 | Add() 21 | elif(ops == "subtract"): 22 | Sub() 23 | else: 24 | Mult() -------------------------------------------------------------------------------- /AddComments!.py: -------------------------------------------------------------------------------- 1 | 2 | #This is a comment 3 | first_name = input("Enter your first name: ") 4 | middle_name = input("Enter your middle name: ") 5 | last_name = input("Enter your last name: ") 6 | 7 | """ 8 | This is also comment 9 | """ 10 | 11 | full_name = first_name + " " + middle_name + " " + last_name 12 | print full_name -------------------------------------------------------------------------------- /AddOne.py: -------------------------------------------------------------------------------- 1 | def add_one(num): 2 | return num + 1 3 | 4 | print add_one(32) -------------------------------------------------------------------------------- /AddParentheses.py: -------------------------------------------------------------------------------- 1 | print 2 + 3 * (4 + 8) -------------------------------------------------------------------------------- /AddingtoaValue.py: -------------------------------------------------------------------------------- 1 | def PrintTotal(): 2 | num2 = int(input("Please enter a number: ")) 3 | tot = num1 + num2 4 | 5 | print str(num1) + " plus " + str(num2) + " is equal to: " + str(tot) 6 | 7 | num1 = 10 8 | 9 | PrintTotal() -------------------------------------------------------------------------------- /Administrators,Teachers,andStudents.py: -------------------------------------------------------------------------------- 1 | role = input("Are you adminastrator, student or teacher: ") 2 | 3 | if role != "adminastrator" and role != "student" and role != "teacher": 4 | print("You can only be an administrator, teacher, or student!") 5 | elif (role == "adminastrator" or role == "teacher"): 6 | print("Administrators and teachers get keys!") 7 | else: 8 | print("Students do not get keys") -------------------------------------------------------------------------------- /Age.py: -------------------------------------------------------------------------------- 1 | x = int(input("Enter your age")) 2 | 3 | print("You will need this many candles for your birthday cake") 4 | print(x + 1) -------------------------------------------------------------------------------- /AgeGroup.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program determines which age group a certain number falls into. 3 | """ 4 | 5 | age = 33 6 | 7 | # Each branch below only gets reached if every prior branch had 8 | # a condition that evaluated to False. 9 | if age < 18: 10 | print "0 - 17" 11 | elif age < 26: 12 | print "18 - 25" 13 | elif age < 36: 14 | print "26 - 35" 15 | elif age < 46: 16 | print "36 - 45" 17 | elif age < 56: 18 | print "46 - 55" 19 | elif age < 66: 20 | print "56 - 65" 21 | else: 22 | print "66+" -------------------------------------------------------------------------------- /AreaofaSquarewithDefaultParameters.py: -------------------------------------------------------------------------------- 1 | def calculate_area(side_length = 10): 2 | area = side_length * side_length 3 | print "The area of a square with sides of length " + str(side_length) + " is " + str(area) 4 | 5 | size = int(input("How big is the square: ")) 6 | 7 | if(size > 0): 8 | calculate_area(size) 9 | else: 10 | calculate_area() -------------------------------------------------------------------------------- /Asterisk.py: -------------------------------------------------------------------------------- 1 | speed(5) 2 | 3 | left(30) 4 | for i in range(6): 5 | forward(100) 6 | backward(100) 7 | left(60) -------------------------------------------------------------------------------- /AverageTestScore,Part2.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program asks for the test scores for multiple people, and reports the average 3 | test score for each person. 4 | """ 5 | 6 | # Try changing these, and see what happens! 7 | num_people = 3 8 | tests_per_person = 3 9 | 10 | # For each person, get name and test scores 11 | for i in range(num_people): 12 | name = input("Enter name: ") 13 | sum = 0 14 | 15 | # Add up the test scores 16 | for j in range(tests_per_person): 17 | score = int(input("Enter a score: ")) 18 | sum = sum + score 19 | 20 | # Calculate and print average score 21 | average = float(sum) / tests_per_person 22 | print "Average for " + name + ": " + str(average) -------------------------------------------------------------------------------- /AverageTestScore.py: -------------------------------------------------------------------------------- 1 | LOOP = 3 2 | total = 0 3 | 4 | for i in range(LOOP): 5 | user_input = float(input(str(i + 1) + ". Please enter your test score: ")) 6 | total = total + user_input 7 | 8 | total = total / LOOP 9 | print ("Average score: " + str(total)) -------------------------------------------------------------------------------- /BasicUserInput.py: -------------------------------------------------------------------------------- 1 | text = input("Enter something: ") 2 | print text 3 | print type(text) -------------------------------------------------------------------------------- /BeadedBracelet.py: -------------------------------------------------------------------------------- 1 | #INSTRUCTIONS 2 | ''' 3 | The bracelet should follow these specifications: 4 | 5 | There must be 36 beads 6 | Each bead should have a radius of 10 pixels 7 | The bracelet must have a diameter of 200 8 | Hints: 9 | 10 | You will need to use a function and a loop in your code! 11 | You will need to turn Tracy 10 degrees after drawing each bead 12 | You should return to the middle of the bracelet circle between drawing each bead 13 | ''' 14 | #End INSTRUCTIONS 15 | 16 | #Early Setup 17 | speed(0) 18 | penup() 19 | left(90) 20 | backward(100) 21 | right(90) 22 | 23 | #Bracelet 24 | 25 | def draw_circle(rad, deg, dist): 26 | pendown() 27 | circle(rad) 28 | left(deg) 29 | penup() 30 | forward(dist) 31 | 32 | for i in range(36): 33 | draw_circle(10, 10, 20) -------------------------------------------------------------------------------- /BikeFrameSize.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program asks a user for the size of their bike frame and determines 3 | if the size fits into the available constraints. 4 | """ 5 | # Continue asking user for a bike frame size until the size fits into constraints 6 | while True: 7 | bike_frame_size = int(input("Enter bike frame size, in cm: ")) 8 | if bike_frame_size > 60: 9 | print "That bike is too big!" 10 | elif bike_frame_size < 55: 11 | print "That bike is too small!" 12 | else: 13 | print "That bike is the right size!" 14 | break 15 | 16 | print "Hooray, I found a bike." -------------------------------------------------------------------------------- /BlackandWhiteSquares.py: -------------------------------------------------------------------------------- 1 | penup() 2 | speed(10) 3 | backward(100) 4 | pendown() 5 | 6 | 7 | def code(): 8 | print("KLJLKASJD:LKASJSSLKJDK:LSJ") 9 | 10 | for i in range(6): 11 | if (i + 1) % 2 == 0: 12 | begin_fill() 13 | 14 | for i in range(4): 15 | forward(20) 16 | left(90) 17 | 18 | code() 19 | end_fill() 20 | penup() 21 | forward(30) 22 | pendown() -------------------------------------------------------------------------------- /BlockPyramid.py: -------------------------------------------------------------------------------- 1 | """ 2 | This code will ask the user how many blocks they want on the bottom of the pyramid 3 | and then draw a pyramid of squares, subtracting one block from each row. 4 | """ 5 | # Set row value to 0 6 | speed(0) 7 | row_value=0 8 | 9 | # This function moves to next row with x-value based on how many blocks are to 10 | # be placed and the y-value based on the row number (gets 50 pixels higher each row) 11 | def move_to_row(num_blocks): 12 | x_value = -((num_blocks*50)/2) 13 | y_value = -200+(50*row_value) 14 | penup() 15 | setposition(x_value,y_value) 16 | pendown() 17 | 18 | # This function draw a row of blocks based on user value 19 | def draw_block_row(num_blocks): 20 | for i in range(num_blocks): 21 | for i in range(4): 22 | forward(50) 23 | left(90) 24 | forward(50) 25 | 26 | # Ask the user how many blocks should be on bottom row 27 | num_blocks=int(input("How many blocks on the bottom row? (8 or less): ")) 28 | 29 | # Call function to move Tracy to beginning of row position and then increase row 30 | # variable value. Then Tracy will draw the row of blocks needed and subtract one 31 | # from the num blocks variable. 32 | for i in range(num_blocks): 33 | move_to_row(num_blocks) 34 | row_value=row_value+1 35 | draw_block_row(num_blocks) 36 | num_blocks=num_blocks-1 -------------------------------------------------------------------------------- /BooleanVariables.py: -------------------------------------------------------------------------------- 1 | is_correct = True 2 | is_validated = False 3 | 4 | print is_correct 5 | print is_validated 6 | 7 | print type(is_correct) 8 | print type(is_validated) -------------------------------------------------------------------------------- /Break.py: -------------------------------------------------------------------------------- 1 | # This for loop only prints 0 through 4. 2 | for i in range(10): 3 | if i == 5: 4 | break 5 | print i -------------------------------------------------------------------------------- /BubbleWrap.py: -------------------------------------------------------------------------------- 1 | """ 2 | This code will fill the canvas with light blue circles. 3 | """ 4 | speed(0) 5 | 6 | # This function will draw a row of 10 circles. 7 | def draw_circle_row(): 8 | for i in range(10): 9 | pendown() 10 | begin_fill() 11 | color("light blue") 12 | circle(20) 13 | end_fill() 14 | penup() 15 | forward(40) 16 | 17 | def move_up_a_row(): 18 | left(90) 19 | forward(40) 20 | right(90) 21 | backward(400) 22 | 23 | # Send Tracy to starting position in bottom left corner. 24 | penup() 25 | setposition(-180,-200) 26 | 27 | # Call circle drawing function 28 | for i in range(10): 29 | draw_circle_row() 30 | move_up_a_row() -------------------------------------------------------------------------------- /BubbleWrap2.0.py: -------------------------------------------------------------------------------- 1 | """ 2 | This code will fill the canvas with light blue circles. 3 | 4 | Now add a function that will draw a white highlight on each bubble. 5 | """ 6 | speed(0) 7 | 8 | # This function will draw one row of 10 circles 9 | def draw_circle_row(): 10 | for i in range(10): 11 | pendown() 12 | begin_fill() 13 | color("light blue") 14 | circle(20) 15 | end_fill() 16 | penup() 17 | forward(40) 18 | make_highlight() 19 | 20 | # This function will move Tracy from end of row up to beginning of the row on top 21 | def move_up_a_row(): 22 | left(90) 23 | forward(40) 24 | right(90) 25 | backward(400) 26 | 27 | def make_highlight(): 28 | penup() 29 | color("white") 30 | backward(400) 31 | forward(10) 32 | for i in range(10): 33 | left(90) 34 | forward(20) 35 | pendown() 36 | circle(10, 90) 37 | penup() 38 | circle(10, 270) 39 | backward(20) 40 | right(90) 41 | forward(40) 42 | backward(10) 43 | 44 | 45 | 46 | # Send Tracy to starting position in bottom left corner 47 | penup() 48 | setposition(-180,-200) 49 | 50 | # Call circle drawing function 10 times to fill ten rows 51 | for i in range(10): 52 | draw_circle_row() 53 | move_up_a_row() -------------------------------------------------------------------------------- /Categories.py: -------------------------------------------------------------------------------- 1 | items = ""; 2 | cat = ""; 3 | 4 | for i in range(3): 5 | items = "" 6 | cat = input("Please enter a catagroy: ") 7 | 8 | for i in range(3): 9 | temp_item = input("Please enter a list item: ") 10 | items = items + temp_item + " " 11 | 12 | print cat + ": " + items -------------------------------------------------------------------------------- /Caterpillar.py: -------------------------------------------------------------------------------- 1 | circle(20); 2 | penup(); 3 | forward(40); 4 | pendown(); 5 | circle(20); 6 | penup(); 7 | forward(40); 8 | pendown(); 9 | circle(20); 10 | penup(); 11 | forward(40); 12 | pendown(); 13 | circle(20); 14 | penup(); 15 | forward(40); 16 | pendown(); 17 | circle(20); 18 | penup(); 19 | forward(40); -------------------------------------------------------------------------------- /CharactersinaString.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program shows how index values are assigned and utilized to isolate 3 | specific characters in a string. 4 | """ 5 | 6 | # indices: 0 1 2 3 4 5 6 7 8 9 10 11 7 | # h e l l o w o r l d ! 8 | # negative indices: -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 9 | my_string = "hello world!" 10 | 11 | print "my_string[0] is " + my_string[0] 12 | print "my_string[1] is " + my_string[1] 13 | print "my_string[2] is " + my_string[2] 14 | 15 | print "my_string[-1] is " + my_string[-1] 16 | print "my_string[-2] is " + my_string[-2] 17 | print "my_string[-3] is " + my_string[-3] -------------------------------------------------------------------------------- /Checkerboard,v1.py: -------------------------------------------------------------------------------- 1 | # Pass this function a list of lists, and it will 2 | # print it such that it looks like the grids in 3 | # the exercise instructions. 4 | def print_board(board): 5 | for i in range(len(board)): 6 | 7 | # This line uses some Python you haven't 8 | # learned yet. You'll learn about this 9 | # part in a future lesson: 10 | # 11 | [str(x) for x in board[i]] 12 | print " ".join([str(x) for x in board[i]]) 13 | 14 | # Your code here... 15 | oneRow = ['1', '1', '1', '1', '1', '1', '1', '1'] 16 | zeroRow = ['0', '0', '0', '0', '0', '0', '0', '0'] 17 | board = [ oneRow, oneRow, oneRow, zeroRow, zeroRow, oneRow, oneRow, oneRow] 18 | 19 | print_board(board) -------------------------------------------------------------------------------- /Checkerboard,v2.py: -------------------------------------------------------------------------------- 1 | # Pass this function a list of lists, and it will 2 | # print it such that it looks like the grids in 3 | # the exercise instructions. 4 | def print_board(board): 5 | for i in range(len(board)): 6 | 7 | if(True): 8 | # This line uses some Python you haven't 9 | # learned yet. You'll learn about this 10 | # part in a future lesson: 11 | # 12 | [str(x) for x in board[i]] 13 | print " ".join([str(x) for x in board[i]]) 14 | 15 | # Your code here... 16 | 17 | row = ['0', '1', '0', '1', '0', '1', '0', '1'] 18 | rowInverted = ['1', '0', '1', '0', '1', '0', '1', '0'] 19 | board = [ row, rowInverted, row, rowInverted, row, rowInverted, row, rowInverted] 20 | 21 | print_board(board) -------------------------------------------------------------------------------- /Checkerboard,v3.py: -------------------------------------------------------------------------------- 1 | # Pass this function a list of lists, and it will 2 | # print it such that it looks like the grids in 3 | # the exercise instructions. 4 | def print_board(board): 5 | for i in range(len(board)): 6 | 7 | if(True): 8 | # This line uses some Python you haven't 9 | # learned yet. You'll learn about this 10 | # part in a future lesson: 11 | # 12 | [str(x) for x in board[i]] 13 | print " ".join([str(x) for x in board[i]]) 14 | 15 | # Your code here... 16 | 17 | row = ['0', '1', '0', '1', '0', '1', '0', '1'] 18 | rowZero = ['0', '0', '0', '0', '0', '0', '0', '0'] 19 | rowInverted = ['1', '0', '1', '0', '1', '0', '1', '0'] 20 | board = [ row, rowInverted, row, rowZero, rowZero, rowInverted, row, rowInverted] 21 | 22 | print_board(board) -------------------------------------------------------------------------------- /Checkerboard.py: -------------------------------------------------------------------------------- 1 | #Instantiation of initial variables 2 | 3 | #Length (Diamiter of each square) 4 | l = 40 5 | #Offset (Distance between squares) 6 | o = 40 7 | 8 | #shift (Color patern shift per row) 9 | s = 0 10 | 11 | #Shifts the turtle up a row 12 | def row_shift(): 13 | penup() 14 | backward(400) 15 | left(90) 16 | forward(l) 17 | right(90) 18 | pendown() 19 | 20 | color_value = input("Please enter a color: ") 21 | 22 | speed(0) 23 | 24 | penup() 25 | setposition(-200, -200) 26 | pendown() 27 | 28 | 29 | for i in range(10): 30 | if (i % 2 == 0): 31 | s = 1 32 | else: 33 | s = 0 34 | 35 | 36 | for x in range(10): 37 | 38 | if ((x + 1 + s) % 2 == 0): 39 | color(color_value) 40 | else: 41 | color("black") 42 | 43 | 44 | begin_fill() 45 | 46 | 47 | for y in range(4): 48 | 49 | forward(l) 50 | left(90) 51 | 52 | 53 | end_fill() 54 | 55 | penup() 56 | forward(o) 57 | pendown() 58 | 59 | row_shift() -------------------------------------------------------------------------------- /CirclePyramid.py: -------------------------------------------------------------------------------- 1 | # speed up the turtle ( waiting is hard) 2 | speed(10) 3 | 4 | # setup 5 | 6 | ''' 7 | # pen is up so we can set the position of the turtle 8 | # 9 | # the setposition command takes in two parameters 10 | # which are the x and y coordinates of where the turtle 11 | # should be placed on the canvas. For the x position 12 | # we have inputed 50 + 50 / 2 because the circles radius 13 | # must be 50 because of the instructions and we are two 14 | # and one half circles away from the 0 coordiante. So one 15 | # half of 50 + 50 is equal to how far away we must go on 16 | # the x axis. This number is multiplied by negitive one 17 | # so we can move to the left side of the canvas instead 18 | # of the right side. 19 | # 20 | # the y axis position is simply - 200 because the size 21 | # of the canvas is 400 x 400 with the dimensions of 22 | # -200, -200, 200, 200. 23 | # 24 | # we then put our pen back down so we can start drawing 25 | # this new position. 26 | ''' 27 | 28 | penup() 29 | setposition((50 + (50 / 2) * 2) * -1, -200) 30 | pendown() 31 | 32 | # end setup 33 | 34 | # draw circles 35 | 36 | # there are three circles on the bottom 37 | for x in range(3): 38 | pendown() 39 | circle(50) 40 | penup() 41 | forward(50 * 2) 42 | 43 | # setup for next row 44 | 45 | setposition((50 + 50) * 0.5 * -1, -200 + 100) 46 | 47 | # draw next row of circles (Same reasoning as before) 48 | for y in range(2): 49 | pendown(); 50 | circle(50) 51 | penup() 52 | forward(50 * 2) 53 | 54 | setposition(0, 0) 55 | pendown() 56 | circle(50) -------------------------------------------------------------------------------- /CirclePyramid2.0.py: -------------------------------------------------------------------------------- 1 | """ 2 | # This code will ask the user how many blocks they want on the bottom of the pyramid 3 | # and then draw a pyramid of squares, subtracting one block from each row. 4 | """ 5 | 6 | ''' 7 | # This function moves to next row with x-value based on how many blocks are to 8 | # be placed and the y-value based on the row number (gets 50 pixels higher each row) 9 | ''' 10 | 11 | #This function moves to the next row up 12 | def move_to_row(num_blocks): 13 | x_value = -(((num_blocks*50)/2)-o) 14 | y_value = -200+(50*row_value) 15 | penup() 16 | setposition(x_value,y_value) 17 | pendown() 18 | 19 | # This function draw a row of blocks based on user value 20 | def draw_block_row(num_blocks): 21 | for i in range(num_blocks): 22 | circle(radius) 23 | penup() 24 | forward(50) 25 | pendown() 26 | 27 | # Set row value to 0 28 | speed(5) 29 | 30 | #Current row and offset defined 31 | row_value = 0 32 | o = 25 33 | 34 | #moving to the starting position 35 | penup() 36 | forward(25) 37 | pendown() 38 | 39 | #radius defined 40 | radius = 25 41 | 42 | # Ask the user how many blocks should be on bottom row 43 | num_blocks=int(input("How many blocks on the bottom row? (8 or less): ")) 44 | 45 | ''' 46 | # Call function to move Tracy to beginning of row position and then increase row 47 | # variable value. Then Tracy will draw the row of blocks needed and subtract one 48 | # from the num blocks variable. 49 | ''' 50 | #=========================== 51 | # MAIN 52 | #=========================== 53 | 54 | for i in range(num_blocks): 55 | move_to_row(num_blocks) 56 | row_value=row_value+1 57 | draw_block_row(num_blocks) 58 | num_blocks=num_blocks-1 -------------------------------------------------------------------------------- /CirclePyramidwithComments.py: -------------------------------------------------------------------------------- 1 | """ 2 | jkhj 3 | this is a multiline coment 4 | """ 5 | 6 | # speed up the turtle ( waiting is hard) 7 | speed(10) 8 | 9 | # setup 10 | 11 | """ 12 | # pen is up so we can set the position of the turtle 13 | # 14 | # the setposition command takes in two parameters 15 | # which are the x and y coordinates of where the turtle 16 | # should be placed on the canvas. For the x position 17 | # we have inputed 50 + 50 / 2 because the circles radius 18 | # must be 50 because of the instructions and we are two 19 | # and one half circles away from the 0 coordiante. So one 20 | # half of 50 + 50 is equal to how far away we must go on 21 | # the x axis. This number is multiplied by negitive one 22 | # so we can move to the left side of the canvas instead 23 | # of the right side. 24 | # 25 | # the y axis position is simply - 200 because the size 26 | # of the canvas is 400 x 400 with the dimensions of 27 | # -200, -200, 200, 200. 28 | # 29 | # we then put our pen back down so we can start drawing 30 | # this new position. 31 | """ 32 | 33 | penup() 34 | setposition((50 + (50 / 2) * 2) * -1, -200) 35 | pendown() 36 | 37 | # end setup 38 | 39 | # draw circles 40 | 41 | # there are three circles on the bottom 42 | for x in range(3): 43 | pendown() 44 | circle(50) 45 | penup() 46 | forward(50 * 2) 47 | 48 | # setup for next row 49 | 50 | setposition((50 + 50) * 0.5 * -1, -200 + 100) 51 | 52 | # draw next row of circles (Same reasoning as before) 53 | for y in range(2): 54 | pendown(); 55 | circle(50) 56 | penup() 57 | forward(50 * 2) 58 | 59 | setposition(0, 0) 60 | pendown() 61 | circle(50) -------------------------------------------------------------------------------- /CircleSquareTriangle.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program draws three shapes from the same position. Each shape is a 3 | different color. 4 | """ 5 | 6 | speed(5) 7 | 8 | # Draws a red circle 9 | color("red") 10 | begin_fill() 11 | circle(75) 12 | end_fill() 13 | 14 | # Draws a blue square due to 4 steps 15 | color("blue") 16 | begin_fill() 17 | circle(75,360,4) 18 | end_fill() 19 | 20 | # Draws a yellow triangle due to 3 steps 21 | color("yellow") 22 | begin_fill() 23 | circle(75,360,3) 24 | end_fill() -------------------------------------------------------------------------------- /CircleinaSquare.py: -------------------------------------------------------------------------------- 1 | left(90) 2 | penup() 3 | backward(100) 4 | 5 | radius = int(input("Rad")) 6 | 7 | def draw(r, d, s): 8 | pendown() 9 | circle(r, d,s) 10 | 11 | right(90) 12 | begin_fill() 13 | color("Red") 14 | for i in range(4): 15 | forward(radius * 2) 16 | left(90) 17 | end_fill() 18 | color("Blue") 19 | penup() 20 | forward(radius) 21 | begin_fill() 22 | circle(radius) 23 | end_fill() -------------------------------------------------------------------------------- /Citation.py: -------------------------------------------------------------------------------- 1 | author_name = ("Martin", "Luther", "King, Jr.") 2 | 3 | print author_name[2] + ", " + author_name[0] + " " + author_name[1] -------------------------------------------------------------------------------- /ColorCodedIncreasingLength.py: -------------------------------------------------------------------------------- 1 | """ 2 | This code will draw hash marks at 25, 50, 100, and 200 pixels. 3 | The color of each hash mark will be determined from user input. 4 | """ 5 | 6 | # Set initial value of length variable to 25 7 | speed(5) 8 | length = 25 9 | 10 | # This function will ask the user for a color. 11 | # It will then draw a hash mark of that color. 12 | def draw_hash_mark(): 13 | color(color_choice) 14 | pensize(mark_thickness) 15 | left(90) 16 | forward(25) 17 | backward(50) 18 | forward(25) 19 | right(90) 20 | color("black") 21 | pensize(1) 22 | 23 | # Send Tracy to starting position 24 | penup() 25 | setposition(-200,0) 26 | pendown() 27 | mark_thickness=int(input("What is the thickness of the marks? (1-20): ")) 28 | 29 | # Move Tracy forward by the value of 'length' and draw a hash mark 30 | # Repeat this 4 times, doubling the length each time 31 | for i in range(4): 32 | forward(length) 33 | color_choice = input("What color should this mark be?: ") 34 | draw_hash_mark() 35 | length = length * 2 -------------------------------------------------------------------------------- /ColoredDartboard.py: -------------------------------------------------------------------------------- 1 | """ 2 | Consists of 4 concentric circles 3 | Has the center circle placed in the middle of the canvas with a radius of 25 4 | Has three circles surrounding the middle, that each increase in radius by 25 pixels 5 | """ 6 | 7 | penup() 8 | left(90) 9 | backward(80) 10 | right(90) 11 | pendown() 12 | 13 | def fill_circle(r): 14 | begin_fill() 15 | color_choice = input("Enter a hexidecimal for next color:") 16 | color(color_choice) 17 | circle(r) 18 | end_fill() 19 | 20 | def calc_circle(a, rad): 21 | if a == 0: 22 | return rad * 4 23 | if a == 1: 24 | return rad * 3 25 | if a == 2: 26 | return rad * 2 27 | if a == 3: 28 | return rad 29 | 30 | def offset_turtle(): 31 | penup() 32 | left(90) 33 | forward(25) 34 | right(90) 35 | pendown() 36 | 37 | for i in range(4): 38 | radius = calc_circle(i, 25) 39 | fill_circle(radius) 40 | offset_turtle() -------------------------------------------------------------------------------- /ColorfulBracelet.py: -------------------------------------------------------------------------------- 1 | #INSTRUCTIONS 2 | ''' 3 | The bracelet should follow these specifications: 4 | 5 | There must be 36 beads 6 | Each bead should have a radius of 10 pixels 7 | The bracelet must have a diameter of 200 8 | Hints: 9 | 10 | You will need to use a function and a loop in your code! 11 | You will need to turn Tracy 10 degrees after drawing each bead 12 | You should return to the middle of the bracelet circle between drawing each bead 13 | ''' 14 | #End INSTRUCTIONS 15 | 16 | #Early Setup 17 | speed(10) 18 | penup() 19 | left(90) 20 | backward(100) 21 | right(90) 22 | 23 | #Bracelet 24 | 25 | def draw_circle(rad, deg, dist): 26 | pendown() 27 | circle(rad) 28 | left(deg) 29 | penup() 30 | forward(dist) 31 | 32 | def multiple_of(x): 33 | if x % 3 == 0: 34 | return 0 35 | if x % 2 == 0: 36 | return 1 37 | if x % 1 == 0: 38 | return 2 39 | 40 | for i in range(36): 41 | print(i) 42 | begin_fill() 43 | if multiple_of(i) == 0: 44 | color("blue") 45 | print("blue") 46 | if multiple_of(i) == 1: 47 | color("red") 48 | print("red") 49 | if multiple_of(i) == 2: 50 | color("purple") 51 | print("purple") 52 | draw_circle(10, 10, 20) 53 | end_fill() -------------------------------------------------------------------------------- /ColorfulCaterpillar.py: -------------------------------------------------------------------------------- 1 | speed(10) 2 | rad = 15 3 | penup() 4 | backward(150) 5 | 6 | def draw_circle(r): 7 | pendown() 8 | begin_fill() 9 | circle(r) 10 | end_fill() 11 | penup() 12 | 13 | for i in range(8): 14 | color_choice = input("Enter A Color Name Or Hexidecimal:") 15 | print color_choice 16 | 17 | color(color_choice) 18 | 19 | draw_circle(rad) 20 | 21 | forward(rad * 2) -------------------------------------------------------------------------------- /CommentedProgram.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program asks the user for a number. It then 3 | prints the result when that number is multiplied 4 | by two. 5 | """ 6 | 7 | # Ask the user for a number 8 | number = int(input("Enter a number: ")) 9 | 10 | # Print the number, doubled 11 | print "Twice that number is: " + str(number * 2) -------------------------------------------------------------------------------- /ComparisonOperators.py: -------------------------------------------------------------------------------- 1 | x = 10 2 | y = 5 3 | 4 | print "x is " + str(x) 5 | print "y is " + str(y) 6 | 7 | print "Is x equal to y?" 8 | print x == y 9 | 10 | print "Is x not equal to y?" 11 | print x != y 12 | 13 | print "Is x less than y?" 14 | print x < y 15 | 16 | print "Is x greater than y?" 17 | print x > y 18 | 19 | print "Is x less than or equal to y?" 20 | print x <= y 21 | 22 | print "Is x greater than or equal to y?" 23 | print x >= y -------------------------------------------------------------------------------- /ConcatenatingTuples.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program shows how to concatenate tuples. 3 | """ 4 | 5 | x = (1, 2) 6 | y = (5, 6) 7 | 8 | my_tuple = x + (3, 4) + y 9 | 10 | print my_tuple 11 | 12 | 13 | my_tuple = x + (3,) + y 14 | 15 | # This should print (1, 2, 3, 5, 6) 16 | print my_tuple -------------------------------------------------------------------------------- /ConcentricCircles.py: -------------------------------------------------------------------------------- 1 | """ 2 | This code will draw concentric circles of any size input as parameters. 3 | """ 4 | 5 | speed(5) 6 | 7 | # This function moves Tracy to the bottom position of a circle based on the radius 8 | # and then draws the circle around the center before moving back to the center 9 | def draw_circle(radius): 10 | right(90) 11 | forward(radius) 12 | left(90) 13 | pendown() 14 | circle(radius) 15 | penup() 16 | setposition(0,0) 17 | 18 | # Ask the user for 3 radiaii and call the draw_circle function with these values 19 | penup() 20 | 21 | first_radius = int(input("What is the first circle's radius?: ")) 22 | second_radius = int(input("What is the second circle's radius?: ")) 23 | third_radius = int(input("What is the third circle's radius?: ")) 24 | 25 | draw_circle(first_radius) 26 | draw_circle(second_radius) 27 | draw_circle(third_radius) -------------------------------------------------------------------------------- /ContainsaLetter.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program usees the keyword 'in' to determine if a certain character or 3 | group of characters exists in a given string. 4 | """ 5 | 6 | word = "eggplant" 7 | 8 | # Check if the letter 'e' is found in the given word 9 | if "e" in word: 10 | print "Found an e in " + word 11 | else: 12 | print "Didn't find an e in " + word 13 | 14 | # Check if the letter 'a' is found in the given word 15 | letter = "a" 16 | if letter in word: 17 | print "Found " + letter + " in " + word 18 | else: 19 | print "Didn't find " + letter + " in " + word 20 | 21 | # Check if the phrase 'egg' is found in the given word 22 | phrase = "egg" 23 | if phrase in word: 24 | print "Found " + phrase + " in " + word 25 | else: 26 | print "Didn't find " + phrase + " in " + word -------------------------------------------------------------------------------- /ContainsaSubstring.py: -------------------------------------------------------------------------------- 1 | """ 2 | Ask the user for a string and something to look for within the string. Output whether 3 | the second string is, in fact, in the first string. 4 | """ 5 | 6 | big_string = input("Enter a string: ") 7 | string_to_find = input("Enter what to look for: ") 8 | 9 | if string_to_find in big_string: 10 | print "Found it!" 11 | else: 12 | print "Didn't find it." -------------------------------------------------------------------------------- /ContainsaVowel.py: -------------------------------------------------------------------------------- 1 | my_string = input("Enter a string of lowercase letters: ") 2 | 3 | vowel = False 4 | 5 | for letter in my_string: 6 | if(letter == "a" or letter == "e" or letter == "i" or letter == "o" or letter == "u"): 7 | vowel = True 8 | 9 | if(vowel): 10 | print "contains a vowel" 11 | else: 12 | print "does not contain a vowel" -------------------------------------------------------------------------------- /Continue.py: -------------------------------------------------------------------------------- 1 | # This for loop only prints 0 through 4 and 6 through 9. 2 | for i in range(10): 3 | if i == 5: 4 | continue 5 | print i -------------------------------------------------------------------------------- /CookoutOrders.py: -------------------------------------------------------------------------------- 1 | 2 | #order_num = (name, burgers, hotdogs) 3 | order_1 = ("Lee", 0, 2) 4 | order_2 = ("Tamia", 1, 0) 5 | order_3 = ("Edward", 1, 1) 6 | 7 | # Your code here... 8 | 9 | print "You need to buy " + str(order_1[1] + order_2[1] + order_3[1]) + " burgers" 10 | print "You need to buy " + str(order_1[2] + order_2[2] + order_3[2]) + " hotdogs" -------------------------------------------------------------------------------- /CoordinatePair.py: -------------------------------------------------------------------------------- 1 | x = int(input('enter x coord>>>')) 2 | y = int(input('enter y coord>>>')) 3 | 4 | coord = (x, y) 5 | 6 | print coord -------------------------------------------------------------------------------- /CoordinatePairs.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | def distance(first_point, second_point): 4 | sqx = (first_point[0] - second_point[0]) ** 2 5 | sqy = (first_point[1] - second_point[1]) ** 2 6 | 7 | expTot = sqx + sqy 8 | 9 | return math.sqrt(expTot) 10 | 11 | 12 | # This should print 5.0 13 | print distance((1, 1), (4, 5)) -------------------------------------------------------------------------------- /CorrectPortion.py: -------------------------------------------------------------------------------- 1 | # Amount of food and number of people 2 | tons_of_food = 0.07 3 | NUM_PEOPLE = 25 4 | 5 | # Determine how much food each person gets 6 | tons_of_food_per_person = tons_of_food / NUM_PEOPLE 7 | 8 | tons_of_food_per_person = round(tons_of_food_per_person, 5) 9 | 10 | print tons_of_food_per_person 11 | 12 | # Ask the user how much food they took 13 | tons_taken = float(input("How many tons of food did you take? ")) 14 | 15 | tons_taken = round(tons_taken, 5) 16 | 17 | 18 | 19 | if tons_of_food_per_person == tons_taken: 20 | print "Good job, you took the right amount of food!" 21 | else: 22 | print "You took the wrong amount of food!" -------------------------------------------------------------------------------- /CountOccurrences.py: -------------------------------------------------------------------------------- 1 | def count_occurrences(line, char): 2 | found = 0 3 | 4 | for letter in line: 5 | if(letter == char): 6 | found = found + 1 7 | 8 | return found 9 | 10 | text = input("please enter a string: ") 11 | 12 | char = input("Enter a second string I guesss: ") 13 | 14 | print count_occurrences(text, char) -------------------------------------------------------------------------------- /Counting10to100byTens.py: -------------------------------------------------------------------------------- 1 | for i in range(1, 11): 2 | print i * 10 -------------------------------------------------------------------------------- /Dartboard.py: -------------------------------------------------------------------------------- 1 | radius = 25 2 | speed(10) 3 | 4 | def drawCircle(): 5 | for i in range(4): 6 | 7 | rad = radius * i + radius 8 | new_rad = radius + rad 9 | 10 | penup() 11 | left(90) 12 | backward(radius) 13 | right(90) 14 | pendown() 15 | 16 | circle(rad) 17 | 18 | drawCircle() -------------------------------------------------------------------------------- /Dartboardusingi.py: -------------------------------------------------------------------------------- 1 | for i in range(4): 2 | circle((i + 1) * 25) 3 | penup() 4 | left(90) 5 | backward(25) 6 | right(90) 7 | pendown() -------------------------------------------------------------------------------- /DashedLine.py: -------------------------------------------------------------------------------- 1 | penup() 2 | backward(200) 3 | pendown() 4 | forward(100) 5 | penup() 6 | forward(100) 7 | pendown() 8 | forward(100) 9 | penup() 10 | forward(100) 11 | pendown() -------------------------------------------------------------------------------- /DefaultParameterValues.py: -------------------------------------------------------------------------------- 1 | # This function prints the numbers given as parameters and uses a 2 | # default value for y if none is given. 3 | def print_two_numbers(x, y = 10): 4 | print "First number: " + str(x) 5 | print "Second number: " + str(y) 6 | 7 | print_two_numbers(5) 8 | print_two_numbers(7, 8) -------------------------------------------------------------------------------- /DigitsofPi.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program uses the math module and the round function to print pi 3 | approximated to different numbers of decimal places. 4 | """ 5 | 6 | import math 7 | 8 | print [round(math.pi, i) for i in range(1, 6)] -------------------------------------------------------------------------------- /Divisibility,Part2.py: -------------------------------------------------------------------------------- 1 | numerator = int(input("Enter a numerator: ")) 2 | denominator = int(input("Enter denominator: ")) 3 | 4 | # If denominator is 0, this will result in a division- 5 | # by-zero error! Add in code to solve this issue: 6 | 7 | try: 8 | if(numerator / denominator * denominator == numerator): 9 | print "Divides evenly!" 10 | else: 11 | print "Doesn't divide evenly." 12 | except: 13 | print "ZeroDivisionError Exception" -------------------------------------------------------------------------------- /Divisibility.py: -------------------------------------------------------------------------------- 1 | numerator = int(input("Enter a numerator: ")) 2 | denominator = int(input("Enter denominator: ")) 3 | 4 | # Use a while loop here to repeatedly ask the user for 5 | # a denominator for as long as the denominator is 0 6 | # (or, put another way, until the denominator is not 7 | # equal to 0). 8 | 9 | while(denominator == 0): 10 | denominator = int(input("Denominator may not be zero! Enter denominator: ")) 11 | 12 | if numerator / denominator * denominator == numerator: 13 | print "Divides evenly!" 14 | else: 15 | print "Doesn't divide evenly." -------------------------------------------------------------------------------- /Divisibleby3.py: -------------------------------------------------------------------------------- 1 | booleanList = [False, False, False, False, False, False, False, False, False, False, ] 2 | numbers = [x % 3 for x in range(1, 10)] 3 | 4 | for i in range(len(numbers)): 5 | if(numbers[i] == 0): 6 | booleanList[i] = True 7 | 8 | print booleanList -------------------------------------------------------------------------------- /Doghouse.py: -------------------------------------------------------------------------------- 1 | my_string = "doghouse" 2 | 3 | # print "h" here 4 | print my_string[3] 5 | 6 | # print "e" here 7 | print my_string[7] 8 | 9 | # print "e" using a second index value 10 | print my_string[-1] -------------------------------------------------------------------------------- /DottedLine.py: -------------------------------------------------------------------------------- 1 | penup() 2 | backward(200) 3 | 4 | for i in range (40): 5 | pendown() 6 | forward(5) 7 | penup() 8 | forward(5) -------------------------------------------------------------------------------- /DoubleForLoop.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program visualizes nested for loops by printing number 0 through 3 3 | and then 0 through 3 for the nested loop. 4 | """ 5 | 6 | for i in range(4): 7 | print "Outer for loop: " + str(i) 8 | for j in range(4): 9 | print " Inner for loop: " + str(j) -------------------------------------------------------------------------------- /EnterNameandAge.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program asks the user for their name and age. It handles the case where the 3 | user fails to enter a valid integer for their age. 4 | """ 5 | 6 | # Ask user for name and age. 7 | # Enter default value for age in case they do not enter an integer 8 | name = input("Enter your name: ") 9 | age = -1 10 | 11 | try: 12 | age = int(input("Enter your age: ")) 13 | except: 14 | print "That wasn't an integer." 15 | 16 | # Print name and age, using default age if user did not enter an integer 17 | print "Name: " + name 18 | print "Age: " + str(age) -------------------------------------------------------------------------------- /EnteraNumber.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program tries to retrieve an integer from the user. If the user enters 3 | something other than an integer, the program handles it by printing an error. 4 | """ 5 | 6 | try: 7 | my_number = int(input("Enter an integer: ")) 8 | print "Your number: " + str(my_number) 9 | except ValueError: 10 | print "That wasn't an integer!" -------------------------------------------------------------------------------- /EnteraPositiveNumber.py: -------------------------------------------------------------------------------- 1 | def retrieve_positive_number(): 2 | while(prompt): 3 | try: 4 | num = float(input("Enter a number: ")) 5 | 6 | if(num >= 0): 7 | return num 8 | else: 9 | print("number can not be negated") 10 | 11 | except ValueError: 12 | print("number not valid try again") -------------------------------------------------------------------------------- /Enthusiasm.py: -------------------------------------------------------------------------------- 1 | userInput = input("say something: ") 2 | 3 | def add_enthusiasm(text): 4 | return text.upper() 5 | 6 | print "You mean " + add_enthusiasm(userInput) + "!" -------------------------------------------------------------------------------- /Exclamat!onPo!nts.py: -------------------------------------------------------------------------------- 1 | content = input("Enter a string bellow:\n") 2 | 3 | content = content.split() 4 | 5 | result = "" 6 | 7 | for word in range(len(content)): 8 | 9 | i_word = content[word] 10 | 11 | for char in range(len(i_word)): 12 | 13 | if(i_word[char] == "i"): 14 | i_word = i_word[0 : char] + "!" + i_word[char + 1 : len(i_word)] 15 | 16 | result = result + i_word 17 | 18 | print result -------------------------------------------------------------------------------- /ExtendedGreeting.py: -------------------------------------------------------------------------------- 1 | """ 2 | This code will print out a greeting based on the time of day. 3 | """ 4 | 5 | # This function will print a greeting for the morning 6 | def morning_greeting(): 7 | print "Good morning! It will be a lovely day!" 8 | 9 | # This function will print a greeting for the afternoon 10 | def afternoon_greeting(): 11 | print "Good afternoon! I hope you are having a lovely day!" 12 | 13 | # This function will print a greeting for the evening 14 | def evening_greeting(): 15 | print "Good evening! I hope you had a lovely day!" 16 | 17 | 18 | # Get input from the user and print matching greeting 19 | # If other option, print that it is invalid 20 | time_of_day = input("What time of day is it? (morning, afternoon, evening): ") 21 | 22 | if time_of_day == "morning": 23 | morning_greeting() 24 | elif time_of_day == "afternoon": 25 | afternoon_greeting() 26 | elif time_of_day == "evening": 27 | evening_greeting() 28 | else: 29 | print "Invalid option." -------------------------------------------------------------------------------- /FindtheError.py: -------------------------------------------------------------------------------- 1 | my_string = "hello!" 2 | 3 | # One of the two lines below will cause an error. 4 | # Each line is an attempt to replace the first 5 | # letter of myString with H. Comment out the 6 | # line you think is incorrect. 7 | #my_string[0] = "H" 8 | my_string = "H" + my_string[1:] 9 | 10 | print my_string -------------------------------------------------------------------------------- /FirstCharacter.py: -------------------------------------------------------------------------------- 1 | def first_character(chars): 2 | return chars[0] 3 | def all_but_first_character(chars): 4 | return chars[1:] 5 | 6 | print first_character("hello") 7 | print all_but_first_character("hello") -------------------------------------------------------------------------------- /FiveNumbers.py: -------------------------------------------------------------------------------- 1 | numList = [] 2 | result = 0 3 | 4 | for i in range(5): 5 | num = int(input("Enter a number: ")) 6 | numList = numList + [num] 7 | print numList 8 | 9 | for i, item in enumerate(numList): 10 | result = result + item 11 | 12 | print "\n\nAddition Result: " + str(result) -------------------------------------------------------------------------------- /FixThisProgram (1).py: -------------------------------------------------------------------------------- 1 | instrument = "kazoo" 2 | age = str(7) 3 | 4 | print "I have played the " + instrument + " since I was " + age + " years old." -------------------------------------------------------------------------------- /FixThisProgram! (1).py: -------------------------------------------------------------------------------- 1 | print "Hi there!" 2 | print "My favorite color is magenta." -------------------------------------------------------------------------------- /FixThisProgram!.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program takes two values and prints their sum. 3 | """ 4 | 5 | def add_nums(): 6 | num1 = 5 7 | num2 = 6 8 | print num1 + num2 9 | 10 | add_nums() -------------------------------------------------------------------------------- /FixThisProgram.py: -------------------------------------------------------------------------------- 1 | can_juggle = True 2 | 3 | # The code below has problems. See if 4 | # you can fix them! 5 | 6 | if can_juggle: 7 | print "I can juggle!" 8 | else: 9 | print "I can't juggle." -------------------------------------------------------------------------------- /FixThisTuple.py: -------------------------------------------------------------------------------- 1 | my_tuple = (0, 1, 2, "hi", 4, 5) 2 | 3 | # Your code here... 4 | 5 | print my_tuple[:3] + (3,) + my_tuple[4:] -------------------------------------------------------------------------------- /ForLoop+WhileLoop.py: -------------------------------------------------------------------------------- 1 | # You can also nest for loops with 2 | # while loops. Check it out! 3 | 4 | for i in range(4): 5 | print "For loop: " + str(i) 6 | x = i 7 | while x >= 0: 8 | print " While loop: " + str(x) 9 | x = x - 1 -------------------------------------------------------------------------------- /ForLoopsandLists,Part2.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program uses for loops to iterate through a list and uses the range 3 | values in the for loop as the index values to print each list element. 4 | """ 5 | 6 | first_list = [1, 10, 3] 7 | second_list = [2, 5, 6] 8 | 9 | # This for loop uses the length of the list to determine the index values 10 | for index in range(len(first_list)): 11 | print "Looking at index: " + str(index) 12 | thing_in_first_list = first_list[index] 13 | thing_in_second_list = second_list[index] 14 | 15 | print " 1st list has: " + str(thing_in_first_list) 16 | print " 2nd list has: " + str(thing_in_second_list) 17 | if (thing_in_first_list > thing_in_second_list): 18 | print " thing in 1st list was bigger" 19 | elif (thing_in_second_list > thing_in_first_list): 20 | print " thing in 2nd list was bigger" -------------------------------------------------------------------------------- /ForLoopsandLists.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program will use for loops to go through elements in a list in 3 | multiple ways. 4 | """ 5 | 6 | my_list = ["hi", 5, 4.7] 7 | 8 | # Print each element in the list 9 | for thing in my_list: 10 | print "The next thing is: " + str(thing) 11 | 12 | # Use the len function to control the range value in your for loop 13 | for index in range(len(my_list)): 14 | print "The thing at index " + str(index) + \ 15 | " is " + str(my_list[index]) 16 | 17 | # Keep track of the index value as well as the element stored there 18 | for index, value in enumerate(my_list): 19 | print "The thing at index " + str(index) + \ 20 | " is " + str(value) -------------------------------------------------------------------------------- /FourCircles.py: -------------------------------------------------------------------------------- 1 | speed(5) 2 | 3 | penup() 4 | setposition(-50,-100) 5 | 6 | for i in range (2): 7 | pendown() 8 | circle(50) 9 | penup() 10 | forward(100) 11 | 12 | setposition(-50,0) 13 | 14 | for i in range (2): 15 | pendown() 16 | circle(50) 17 | penup() 18 | forward(100) -------------------------------------------------------------------------------- /FourCircleswithComments.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program will draw four circles in a square formation at the center of the 3 | canvas. Each circle will have a radius of 50. 4 | """ 5 | speed(5) 6 | 7 | # Move to bottom left of circle group at position (-50,-100) 8 | penup() 9 | setposition(-50,-100) 10 | 11 | # Draw two circles next to each other 12 | for i in range (2): 13 | pendown() 14 | circle(50) 15 | penup() 16 | forward(100) 17 | 18 | # Move to top of circle row at position (-50, 0) 19 | setposition(-50,0) 20 | 21 | # Draw two circles next to each other 22 | for i in range (2): 23 | pendown() 24 | circle(50) 25 | penup() 26 | forward(100) -------------------------------------------------------------------------------- /FourColoredTriangles.py: -------------------------------------------------------------------------------- 1 | """ 2 | Have sides of different colors (red, green and blue!) 3 | Have sides that are 50 pixels long 4 | Are centered around the y-axis 5 | Have sides drawn with a thickness of 5 pixels 6 | Hints: 7 | 8 | Tracy is drawing 4 triangles, which means that 2 should be left of center and 2 should be right of center. Each triangle has a width of 50. This should help you figure out where to place Tracy to center your triangles! 9 | Try to figure out how you can use a for loop to shorten your code! 10 | """ 11 | color("red") 12 | pensize(5) 13 | forward(100) 14 | backward(200) 15 | 16 | def draw_triangle_hump(): 17 | color("green") 18 | left(60) 19 | forward(50) 20 | right(120) 21 | color("blue") 22 | forward(50) 23 | left(60) 24 | 25 | for x in range(4): 26 | draw_triangle_hump() -------------------------------------------------------------------------------- /FourCorners.py: -------------------------------------------------------------------------------- 1 | speed(10) 2 | penup() 3 | setposition(-200, -200) 4 | 5 | square_length = int(input("Square Size: ")) 6 | 7 | def draw(d): 8 | pendown() 9 | for x in range(4): 10 | forward(d) 11 | left(90) 12 | penup() 13 | 14 | draw(square_length) 15 | setposition(200, 200) 16 | right(180) 17 | draw(square_length) 18 | setposition(200, -200) 19 | right(90) 20 | draw(square_length) 21 | setposition(-200, 200) 22 | right(180) 23 | draw(square_length) -------------------------------------------------------------------------------- /FruitsandVegetables.py: -------------------------------------------------------------------------------- 1 | my_list = ["broccoli", "apple", "zucchini", "rambutan", "grapefruit"] 2 | 3 | my_list.remove("grapefruit") 4 | my_list.sort() 5 | my_list.reverse() 6 | 7 | 8 | print my_list -------------------------------------------------------------------------------- /FullName&Citation.py: -------------------------------------------------------------------------------- 1 | firstName = input('Enter your first name>>>').strip() 2 | lastName = input('Enter your last name>>>').strip() 3 | 4 | print "FUll name: " + firstName + ' ' + lastName 5 | 6 | firstName, lastName = lastName, firstName 7 | 8 | print "Citation: " + firstName + ', ' + lastName -------------------------------------------------------------------------------- /FunctionCallsAsParameters.py: -------------------------------------------------------------------------------- 1 | # The return value of one function can be used as the parameter of another. This 2 | # is called function composition. 3 | 4 | def add_two(x): 5 | return x + 2 6 | 7 | def multiply_by_three(x): 8 | return x * 3 9 | 10 | def my_function(x): 11 | return add_two(multiply_by_three(x)) 12 | 13 | print my_function(5) -------------------------------------------------------------------------------- /FunctionsCallingFunctions.py: -------------------------------------------------------------------------------- 1 | # A function can call another function! 2 | 3 | def add_two(x): 4 | return x + 2 5 | 6 | def multiply_by_three(x): 7 | return x * 3 8 | 9 | def my_function(x): 10 | return add_two(x) + multiply_by_three(x) 11 | 12 | print my_function(12) -------------------------------------------------------------------------------- /FunctionsandVariables,Part2.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program shows how changing an outside variable from within a function 3 | makes another variable! 4 | 5 | In this program, print_something has its own variable called z. It can no longer 6 | access the outer variable called z. 7 | """ 8 | 9 | z = 6.5 10 | 11 | def print_something(): 12 | z = "hi" 13 | print z * 3 14 | 15 | print_something() 16 | 17 | print z * 3 -------------------------------------------------------------------------------- /FunctionsandVariables.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program prints a variable saved in a function. 3 | """ 4 | 5 | def print_something(): 6 | x = 10 7 | print x 8 | 9 | print_something() -------------------------------------------------------------------------------- /Geometry.py: -------------------------------------------------------------------------------- 1 | """ 2 | This code will draw 9 shapes in a line, increasing from 0 points to 9. 3 | """ 4 | speed(5) 5 | 6 | # Send Tracy to starting position 7 | penup() 8 | setposition(-180,0) 9 | 10 | # Draw shapes where i controls the number of points and then move forward 11 | for i in range(10): 12 | pendown() 13 | circle(20,360,i) 14 | penup() 15 | forward(40) -------------------------------------------------------------------------------- /Geometry2.0.py: -------------------------------------------------------------------------------- 1 | speed(10) 2 | 3 | for i in range(7): 4 | circle(20 * (i + 1), 360, i) -------------------------------------------------------------------------------- /Greeting.py: -------------------------------------------------------------------------------- 1 | # This function prints a greeting 2 | def greeting(): 3 | print "Hi there!" 4 | print "Nice to meet you!" 5 | 6 | # Call the function to run 7 | greeting() -------------------------------------------------------------------------------- /Grid.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program show how the multiplication operator can be used on lists! It 3 | takes whatever is in the list and repeats it the specified number of times. 4 | """ 5 | 6 | my_grid = [] 7 | 8 | for i in range(8): 9 | my_grid.append([0] * 8) 10 | 11 | print my_grid -------------------------------------------------------------------------------- /GroupsofCharacters.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program shows how index values are assigned and utilized to isolate 3 | multiple characters in a string. 4 | """ 5 | 6 | # indices: 0 1 2 3 4 5 6 7 8 9 10 11 7 | # h e l l o w o r l d ! 8 | # negative indices: -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 9 | my_string = "hello world!" 10 | 11 | print "my_string[1:3] is " + my_string[1:3] 12 | print "my_string[1:] is " + my_string[1:] 13 | print "my_string[:3] is " + my_string[:3] 14 | 15 | print my_string[3:5] + my_string[2] + my_string[5:9] + my_string[-2:] -------------------------------------------------------------------------------- /GuessMyNumber.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program repeatedly asks a user to guess a number. The program ends 3 | when they're guessed correctly. 4 | """ 5 | 6 | # Secret number 7 | my_number = 10 8 | 9 | # Ask the user to guess 10 | guess = int(input("Enter a guess: ")) 11 | 12 | # Keep asking until the guess becomes equal to the secret number 13 | while guess != my_number: 14 | print "Guess again!" 15 | guess = int(input("Enter a guess: ")) 16 | 17 | print "Good job, you got it!" -------------------------------------------------------------------------------- /GuessaNumber.py: -------------------------------------------------------------------------------- 1 | speed(0) 2 | secret_number = 5 3 | user_number = "null" 4 | 5 | while (user_number != secret_number): 6 | user_number = int(input("Please guess a number 1-10: ")) 7 | 8 | print("Congradulations! You won") 9 | color("green") 10 | penup() 11 | left(90) 12 | backward(50) 13 | right(90 + 45) 14 | pendown() 15 | pensize(10) 16 | backward(50) 17 | forward(50) 18 | left(90) 19 | forward(10) -------------------------------------------------------------------------------- /GuessaNumber2.0.py: -------------------------------------------------------------------------------- 1 | #This function draws a checkmark 2 | def draw_checkmark(): 3 | color("green") 4 | pensize(10) 5 | penup() 6 | backward(50) 7 | right(45) 8 | pendown() 9 | forward(50) 10 | left(90) 11 | forward(100) 12 | 13 | 14 | #This function draws the up arrow 15 | def draw_up(): 16 | left(90) 17 | forward(100) 18 | left(45) 19 | backward(50) 20 | forward(50) 21 | right(90) 22 | backward(50) 23 | forward(50) 24 | left(45) 25 | backward(100) 26 | 27 | #This function draws the down arrow 28 | def draw_down(): 29 | right(90) 30 | forward(100) 31 | left(45) 32 | backward(50) 33 | forward(50) 34 | right(90) 35 | backward(50) 36 | forward(50) 37 | left(45) 38 | backward(100) 39 | 40 | 41 | #===========MAIN CODE============ 42 | 43 | user_number = 0 44 | 45 | while user_number != 5: 46 | 47 | input("Press enter to continue...") 48 | 49 | reset() 50 | speed(0) 51 | 52 | user_number = int(input("Guess a number 1-10 only: ")) 53 | 54 | if user_number > 5: 55 | draw_down() 56 | elif user_number < 5: 57 | draw_up() 58 | 59 | draw_checkmark() 60 | print("You won!!! Now go do something productive!") -------------------------------------------------------------------------------- /HappyFace.py: -------------------------------------------------------------------------------- 1 | 2 | happy = input("Are you happy? ") 3 | 4 | def stupid_function(): 5 | print("I AM STUPID") 6 | 7 | if happy == "YES": 8 | penup() 9 | left(90) 10 | backward(100) 11 | right(90) 12 | color("yellow") 13 | pendown() 14 | begin_fill() 15 | circle(100) 16 | end_fill() 17 | penup() 18 | left(90) 19 | color("black") 20 | forward(155) 21 | left(90) 22 | backward(40) 23 | pendown() 24 | begin_fill() 25 | circle(10) 26 | end_fill() 27 | penup() 28 | forward(80) 29 | pendown() 30 | begin_fill() 31 | circle(10) 32 | end_fill() 33 | penup() 34 | left(90) 35 | forward(100) 36 | pendown() 37 | pensize(5) 38 | circle(40, 180) 39 | stupid_function() -------------------------------------------------------------------------------- /Hello,world!.py: -------------------------------------------------------------------------------- 1 | print "Hello, world!" -------------------------------------------------------------------------------- /Hello.py: -------------------------------------------------------------------------------- 1 | name = input("What is your name: ") 2 | print ("Hello") 3 | print (name) -------------------------------------------------------------------------------- /Hexagon.py: -------------------------------------------------------------------------------- 1 | # increase the speed of the turtle, I hate waiting. 2 | speed(5) 3 | 4 | # loop six times because a hbexigon has 6 sides 5 | 6 | for x in range(6): 7 | 8 | #must move forward 50 pixels acording to instructins 9 | forward(50) 10 | 11 | ''' 12 | # the of all the angles of a hexigon is 360, so each angle 13 | # must be at 360 / divided by how many angles there are in 14 | # a hexigon. There are six so we turn left(360 / 6). The 15 | # left command is a method and we input the equation into 16 | # that method! 17 | ''' 18 | 19 | left(360 / 6) -------------------------------------------------------------------------------- /HighJump.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program determines if Yelena can clear the bar in the high jump event 3 | """ 4 | 5 | # Set the bar 6 | height_of_bar = 1.9 7 | print "The bar is " + str(height_of_bar) + " meters off the ground." 8 | 9 | # Yelena jumps! 10 | yelena_jump = 2.0 11 | print "Yelena's jump: " + str(yelena_jump) + " meters." 12 | if yelena_jump > height_of_bar: 13 | print "Yelena cleared the bar!" 14 | else: 15 | print "Yelena didn't clear the bar." -------------------------------------------------------------------------------- /HowManyNames? (1).py: -------------------------------------------------------------------------------- 1 | def Input(): 2 | 3 | invalid = True 4 | 5 | while(invalid): 6 | try: 7 | nameCount = int(input("Enter how many names you have: ")) 8 | invalid = False 9 | except ValueError: 10 | print "Input must be of type int32\n" 11 | 12 | _names = "" 13 | for i in range(nameCount): 14 | _name = input(str(i + 1) + ". Enter name: ") 15 | _names = _names + " " + _name.strip() 16 | 17 | return _names.split() 18 | 19 | names = Input() 20 | 21 | print "First name: " + names[0] 22 | 23 | names.remove(names[0]) 24 | 25 | middleNames = "" 26 | 27 | for i, item in enumerate(names): 28 | if(i == len(names) - 1): break 29 | middleNames = middleNames + item + " " 30 | 31 | print "Middle names: " + middleNames 32 | 33 | print "Last name: " + names[len(names) - 1] -------------------------------------------------------------------------------- /HowManyNames?.py: -------------------------------------------------------------------------------- 1 | names = int(input("How many names do you have? ")) 2 | name = "" 3 | full_name = "" 4 | 5 | for i in range(names): 6 | name = input(str(i + 1) + "Please enter your name: ") 7 | full_name = full_name + " " + name 8 | 9 | print("Your full name is:" + full_name) -------------------------------------------------------------------------------- /If-ElseStatement.py: -------------------------------------------------------------------------------- 1 | has_dog = False 2 | 3 | print "Do I have to walk the dog?" 4 | 5 | # If has_dog is True, the print on line 8 6 | # happens. 7 | if has_dog: 8 | print "Yup." 9 | 10 | # Otherwise, the print on line 12 happens. 11 | else: 12 | print "Nope. I don't have a dog!" -------------------------------------------------------------------------------- /IncreasingCircles.py: -------------------------------------------------------------------------------- 1 | """ 2 | This code will have Tracy draw circles from the bottom of the canvas. The first 3 | circle will have a radius of 10 and the radii will increase by 10 for each. The 4 | code will stop when the circle reaches a radius value of 200. 5 | """ 6 | 7 | #Set radius value to 10 8 | speed(10) 9 | radius = 10 10 | 11 | #Send Tracy to starting position at bottom middle of canvas 12 | penup() 13 | setposition(0,-200) 14 | pendown() 15 | 16 | #While the radius value is less than or equal to 200, draw circles. Increase 17 | #the radius value by 10 on each iteration. 18 | while radius <= 200: 19 | circle(radius) 20 | radius = radius + 10 -------------------------------------------------------------------------------- /IncreasingLength.py: -------------------------------------------------------------------------------- 1 | """ 2 | This code will draw hash marks at 25, 50, 100, and 200 pixels. 3 | """ 4 | 5 | # Set initial value of length variable to 25 6 | speed(5) 7 | length = 25 8 | 9 | # This function will draw a hash mark 10 | def draw_hash_mark(): 11 | left(90) 12 | forward(25) 13 | backward(50) 14 | forward(25) 15 | right(90) 16 | 17 | # Send Tracy to starting position 18 | penup() 19 | setposition(-200,0) 20 | pendown() 21 | 22 | # Move Tracy forward by the value of 'length' and draw a hash mark 23 | # Repeat this 4 times, doubling the length each time 24 | for i in range(4): 25 | forward(length) 26 | draw_hash_mark() 27 | length = length * 2 -------------------------------------------------------------------------------- /IncreasingSquares.py: -------------------------------------------------------------------------------- 1 | length = 50 2 | speed(0) 3 | 4 | while length <= 400: 5 | penup() 6 | backward(25) 7 | right(90) 8 | backward(25) 9 | left(90) 10 | pendown() 11 | 12 | 13 | for i in range(4): 14 | forward(length) 15 | right(90) 16 | 17 | length = length + 50 -------------------------------------------------------------------------------- /Initials.py: -------------------------------------------------------------------------------- 1 | FirstName = input("Please enter your first name: ") 2 | LastName = input("Please enter your last name: ") 3 | 4 | print "Initials: " + FirstName[0] + "." + LastName[0] + "." -------------------------------------------------------------------------------- /IntroduceYourself,Part2.py: -------------------------------------------------------------------------------- 1 | name = "IBXCODECAT" 2 | age = 16 3 | print("Hi my name is " + name + ". I am " + str(age) + " years old!") -------------------------------------------------------------------------------- /IntroduceYourself.py: -------------------------------------------------------------------------------- 1 | print "Hello my name is IBXCODECAT" 2 | print "I like programming and I am a game dev" -------------------------------------------------------------------------------- /IsItRaining?.py: -------------------------------------------------------------------------------- 1 | is_raining = True 2 | 3 | if is_raining: 4 | print("I'm going to dance in the rain!") 5 | else: 6 | print("I'm going to dance in the sun!") -------------------------------------------------------------------------------- /KeysandValues.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program shows how keys are used in dictionaries. 3 | """ 4 | 5 | # Create an empty dictionary 6 | my_dictionary = {} 7 | 8 | # Set particular keys and values directly 9 | my_dictionary["Jonathan"] = 100 10 | my_dictionary["Karel"] = 200 11 | my_dictionary[100] = 2 12 | 13 | print my_dictionary 14 | 15 | # Use the keys to print the associated values 16 | print my_dictionary["Jonathan"] 17 | print my_dictionary["Karel"] 18 | 19 | # Can you guess what this line will print?? 20 | print my_dictionary[my_dictionary["Jonathan"]] -------------------------------------------------------------------------------- /Kid'sShapesToy.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Tackle this problem one shape at a time. You should write four different functions to keep your code as readable as possible! 3 | To draw a circle, do not use extended parameters. Ideally our circle would have an infinite number of points, and the only way to simulate this is to leave point values off completely. 4 | Your end result does not have to exaclty match the given solution, but should include all of the same features. 5 | ''' 6 | 7 | speed(0) 8 | penup() 9 | 10 | def draw_square(): 11 | setposition(-100, 50) 12 | print("setposition: -100, 50") 13 | color("red") 14 | #left(45) 15 | pendown() 16 | begin_fill() 17 | circle(60, 360, 4) 18 | end_fill() 19 | 20 | def draw_circle(): 21 | pendown() 22 | color("blue") 23 | begin_fill() 24 | circle(60) 25 | end_fill() 26 | 27 | def draw_pentigon(): 28 | pendown() 29 | color("green") 30 | begin_fill() 31 | circle(60, 360, 5) 32 | end_fill() 33 | 34 | def draw_moon(): 35 | pendown() 36 | color("yellow") 37 | begin_fill() 38 | circle(60,180) 39 | left(90) 40 | forward(120) 41 | end_fill() 42 | 43 | draw_square() 44 | penup() 45 | forward(200) 46 | draw_circle() 47 | penup() 48 | right(90) 49 | forward(200) 50 | left(90) 51 | draw_pentigon() 52 | penup() 53 | backward(200) 54 | draw_moon() -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 IBXCODECAT 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LastNames.py: -------------------------------------------------------------------------------- 1 | names = [ 2 | "Maya Angelou", 3 | "Chimamanda Ngozi Adichie", 4 | "Tobias Wolff", 5 | "Sherman Alexie", 6 | "Aziz Ansari" 7 | ] 8 | 9 | print [name.split()[-1] for name in names] -------------------------------------------------------------------------------- /LengthofUser'sName.py: -------------------------------------------------------------------------------- 1 | name = input("Enter your name: ") 2 | 3 | print "Your name has " + str(len(name)) + " letters." -------------------------------------------------------------------------------- /LettersinName.py: -------------------------------------------------------------------------------- 1 | name = input("What is your name: ") 2 | 3 | print list(name) -------------------------------------------------------------------------------- /Librarian,Part2.py: -------------------------------------------------------------------------------- 1 | nameList = [] 2 | lastNames = [] 3 | 4 | for i in range(5): 5 | name = input("Enter your full name: ") 6 | nameList.append(name) 7 | 8 | for i in range(5): 9 | lastNames.append(nameList[-1]) 10 | 11 | lastNames.sort() 12 | 13 | print lastNames -------------------------------------------------------------------------------- /Librarian.py: -------------------------------------------------------------------------------- 1 | bookList = [] 2 | 3 | for i in range(5): 4 | book = input("Enter book: ") 5 | bookList = bookList + [book] 6 | 7 | bookList.sort() 8 | 9 | print bookList -------------------------------------------------------------------------------- /LineofIncreasingBlocks.py: -------------------------------------------------------------------------------- 1 | #sets position 2 | penup() 3 | speed(5) 4 | setposition(-150,0) 5 | pendown() 6 | 7 | #defines the function to draw one square 8 | def draw_square(): 9 | for i in range(4): 10 | forward(sidelength) 11 | left(90) 12 | 13 | #changes the distance in between each sidelength everytime 14 | def move(): 15 | forward(sidelength*2) 16 | sidelength= 10 17 | 18 | #repeats drawing the square and moving 19 | for i in range (5): 20 | draw_square() 21 | penup() 22 | move() 23 | sidelength= sidelength + 10 24 | pendown() -------------------------------------------------------------------------------- /ListedGreeting.py: -------------------------------------------------------------------------------- 1 | info = input("Enter name, age, sport: ") 2 | 3 | info = list(info.split()) 4 | 5 | print "Hello, " + "".join(info[0]) + "! I also enjoy " + "".join(info[2]) + "!" -------------------------------------------------------------------------------- /ListofBooleans.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program creates a list of booleans from the original list, where True 3 | is given if the number is even and False otherwise. 4 | """ 5 | 6 | original_list = [10, 6, -43, 4, 3, 4, 0, 50] 7 | print [x % 2 == 0 for x in original_list] -------------------------------------------------------------------------------- /ListofConsecutiveNumbers.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program shows how range can be used in a list comprehension to make a 3 | list out of a range. 4 | """ 5 | 6 | numbers = [x for x in range(5)] 7 | print numbers -------------------------------------------------------------------------------- /ListofSquares.py: -------------------------------------------------------------------------------- 1 | """ 2 | This example prints a list of the first five square numbers, staring at 1. 3 | """ 4 | 5 | numbers = [x * x for x in range(1, 6)] 6 | print numbers -------------------------------------------------------------------------------- /ListofTuples,TuplesofLists.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program displays the rules of modifying lists and tuples. 3 | """ 4 | 5 | # Here we have a list with a tuple embedded 6 | my_list = ["a", "b", "c", (1, 2, 3)] 7 | print my_list 8 | 9 | # You can alter the list this way because you are allowed to replace a tuple 10 | # and you are allowed to change a list. 11 | my_list[3] = (10, 2, 3) 12 | print my_list 13 | 14 | 15 | # Here we have a tuple with a list embedded 16 | my_tuple = ("a", "b", "c", [1, 2, 3]) 17 | print my_tuple 18 | 19 | # You can alter the tuple by changing the element inside the list because 20 | # you are allowed to change a list. 21 | my_tuple[3][0] = 10 22 | print my_tuple -------------------------------------------------------------------------------- /LogicalOperators.py: -------------------------------------------------------------------------------- 1 | x = True 2 | y = False 3 | 4 | print "x: " + str(x) 5 | print "y: " + str(y) 6 | 7 | print "x and y" 8 | print x and y 9 | 10 | print "x or y" 11 | print x or y 12 | 13 | print "x and not y" 14 | print x and not y 15 | 16 | # This means "It is not the case 17 | # that x is true, and it is the 18 | # case that y is true." 19 | print "not x and y" 20 | print not x and y 21 | 22 | # Notice that you can use parentheses 23 | # to force (x and y) to be evaluated 24 | # before the "not" keyword is applied. 25 | # 26 | # This means "It is not the case that 27 | # both x and y are true." 28 | print "not (x and y)" 29 | print not (x and y) -------------------------------------------------------------------------------- /LoopandaHalf.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program continually asks a user to guess a number until they have guessed 3 | the magic number. 4 | """ 5 | 6 | magic_number = 10 7 | 8 | # Ask use to enter a number until they guess correctly 9 | # When they guess the right answer, break out of loop 10 | while True: 11 | guess = int(input("Guess my number: ")) 12 | if guess == magic_number: 13 | print "You got it!" 14 | break 15 | print "Try again!" 16 | 17 | # Print this sentence once number has been guessed 18 | print "Great job guessing my number!" -------------------------------------------------------------------------------- /Lower.py: -------------------------------------------------------------------------------- 1 | magic_number = 3 2 | 3 | while True: 4 | x = int(input("guess a number: ")) 5 | 6 | if(x == magic_number): 7 | print "Correct!" 8 | break 9 | 10 | if(x > magic_number): 11 | print "Too High!" 12 | continue 13 | else: 14 | print "Too Low!" 15 | continue 16 | 17 | 18 | print "Try Again!" -------------------------------------------------------------------------------- /Lower2.0.py: -------------------------------------------------------------------------------- 1 | magic_number = 3.3312 2 | 3 | while True: 4 | x = float(input("guess a number: ")) 5 | 6 | if(x == magic_number): 7 | print "Correct!" 8 | break 9 | 10 | if(x > magic_number): 11 | print "Too High!" 12 | continue 13 | else: 14 | print "Too Low!" 15 | continue 16 | 17 | 18 | print "Try Again!" -------------------------------------------------------------------------------- /MakeSomeVariables!.py: -------------------------------------------------------------------------------- 1 | i = int(1) 2 | s = str("jojo") 3 | 4 | print i 5 | print s -------------------------------------------------------------------------------- /MaxInList.py: -------------------------------------------------------------------------------- 1 | def max_int_in_list(_list): 2 | maxi = _list[0] 3 | 4 | for index, value in enumerate(_list): 5 | if(value > maxi): 6 | maxi = value 7 | 8 | return maxi 9 | 10 | my_list = [5, 2, -5, 10, 23, -21, 22] 11 | biggest_int = max_int_in_list(my_list) 12 | print biggest_int -------------------------------------------------------------------------------- /NameandAge.py: -------------------------------------------------------------------------------- 1 | """ 2 | This function takes two arguments. It expects the first to be a string and 3 | the second to be a number. 4 | """ 5 | 6 | def print_name_and_age(name, age): 7 | print "Hi, my name is " + name + " and I am " + str(age) + " years old." 8 | 9 | print_name_and_age("Sierra", 34) 10 | print_name_and_age("Marcus", 19) -------------------------------------------------------------------------------- /Negate.py: -------------------------------------------------------------------------------- 1 | # Here's a function that has a parameter and a return value. It takes the 2 | # parameter and returns the negative of it. 3 | def negate(x): 4 | return -x 5 | 6 | print negate(5) 7 | 8 | x = negate(-4) 9 | print x -------------------------------------------------------------------------------- /OldEnoughtoVote?.py: -------------------------------------------------------------------------------- 1 | age = int(input("How old are you? ")) 2 | 3 | print "Age " + str(age) 4 | 5 | if age >= 18: 6 | print("You are old enough to vote") 7 | else: 8 | print("You are not old enough to vote") -------------------------------------------------------------------------------- /OperatorPrecedence.py: -------------------------------------------------------------------------------- 1 | print -5 + 4 * 2 % 3 - 1 2 | print -5 + 4 * 2 % (3 - 1) 3 | 4 | x = 3 5 | y = 7 6 | z = x * - (y - 1) % 4 7 | 8 | print z -------------------------------------------------------------------------------- /OperatorsandFloats.py: -------------------------------------------------------------------------------- 1 | x = 4.1 2 | y = 5.2 3 | z = 6 4 | 5 | print "x + y:" 6 | print x + y 7 | print " " 8 | print "z * 3:" 9 | print z * 3 10 | print " " 11 | print "x - z:" 12 | print x - z 13 | print " " 14 | print "z / x:" 15 | print z / x 16 | print " " 17 | print "y ** x:" 18 | print y ** x 19 | print " " 20 | print "z % y:" 21 | print z % y 22 | print " " 23 | print "-x:" 24 | print -x -------------------------------------------------------------------------------- /OperatorsandIntegers.py: -------------------------------------------------------------------------------- 1 | x = 4 2 | y = 5 3 | z = 6 4 | 5 | print "x + y:" 6 | print x + y 7 | print " " 8 | print "z * 3:" 9 | print z * 3 10 | print " " 11 | print "x - z:" 12 | print x - z 13 | print " " 14 | print "z / x:" 15 | print z / x 16 | print " " 17 | print "y ** x:" 18 | print y ** x 19 | print " " 20 | print "z % y:" 21 | print z % y 22 | print " " 23 | print "-x:" 24 | print -x -------------------------------------------------------------------------------- /OperatorsandStrings.py: -------------------------------------------------------------------------------- 1 | print "hello " + ", world!" 2 | print "a" + "b" + "c" 3 | print "hi" * 3 4 | 5 | print "hi" + str(3) 6 | print "My bike has " + str(6) + " gears." -------------------------------------------------------------------------------- /Owls,Part2.py: -------------------------------------------------------------------------------- 1 | KEY = "owl" 2 | itemCount = 0 3 | itemIndecies = [] 4 | 5 | search = input("Please type a paragraph bellow:\n") 6 | 7 | i_search = search.strip().lower() 8 | i_content = i_search.split() 9 | 10 | for index, item in enumerate(i_content): 11 | if(item.find(KEY) != -1): 12 | itemCount = itemCount + 1 13 | itemIndecies = itemIndecies + [index] 14 | 15 | print "There were " + str(itemCount) + " words that contained \"" + KEY + "\".\nThey occurred at indices: " + str(itemIndecies) -------------------------------------------------------------------------------- /Owls.py: -------------------------------------------------------------------------------- 1 | KEY = "owl" 2 | itemCount = 0 3 | 4 | search = input("Please type a paragraph bellow:\n") 5 | 6 | i_search = search.strip().lower() 7 | i_content = i_search.split() 8 | 9 | for item in i_content: 10 | if(item.find(KEY) != -1): 11 | itemCount = itemCount + 1 12 | 13 | print "There were " + str(itemCount) + " words that contained \"" + KEY + "\"" -------------------------------------------------------------------------------- /Packing.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program shows how packing can use variables as elements in a list. 3 | """ 4 | 5 | x = 1 6 | y = 2 7 | z = 3 8 | my_list = [x, y, z] 9 | 10 | print my_list -------------------------------------------------------------------------------- /Part1,RemoveAllFromString.py: -------------------------------------------------------------------------------- 1 | def remove_all_from_string(char, word): 2 | 3 | word.find(char) 4 | 5 | final_string = word.replace(char, "") 6 | return final_string 7 | 8 | printString = remove_all_from_string("l", "hello") 9 | 10 | print printString -------------------------------------------------------------------------------- /Part1,ReplaceaLetter.py: -------------------------------------------------------------------------------- 1 | def replace_at_index(old, index): 2 | 3 | string = list(old) 4 | 5 | string[index] = "-"; 6 | 7 | returnString = "" 8 | 9 | for i in range(8): 10 | returnString = returnString + string[i] 11 | 12 | return returnString 13 | 14 | print replace_at_index("eggplant", 3) -------------------------------------------------------------------------------- /Part2,ReplaceaLetter.py: -------------------------------------------------------------------------------- 1 | def replace_at_index(old, new, index): 2 | 3 | string = list(old) 4 | 5 | string[index] = new; 6 | 7 | returnString = "" 8 | 9 | for i in range(len(old)): 10 | returnString = returnString + string[i] 11 | 12 | return returnString 13 | 14 | phrase = input("Please enter a phrase: ") 15 | index = int(input("Plase enter an index: ")) 16 | char = input("Please enter new char: ") 17 | 18 | print replace_at_index(phrase, char[0], index) -------------------------------------------------------------------------------- /PhoneBook.py: -------------------------------------------------------------------------------- 1 | names = {} 2 | 3 | inputl = True 4 | while(inputl): 5 | name = input('Enter a name to lookup or file>>>').strip() 6 | 7 | if(name == ''): 8 | inputl = False 9 | else: 10 | if name in names: 11 | print names[name] 12 | else: 13 | 14 | invalid = True 15 | while(invalid): 16 | 17 | num = input('Enter the phone number to file>>>') 18 | 19 | if(not num.isnumeric()): 20 | print 'phone number must be numeric' 21 | else: 22 | if(len(num) != 10): 23 | print 'phone number must be ten chars long' 24 | else: 25 | invalid = False 26 | names[name] = num 27 | 28 | print "buh bye" -------------------------------------------------------------------------------- /PhoneSignal.py: -------------------------------------------------------------------------------- 1 | for i in range(5): 2 | for x in range(2): 3 | forward(10) 4 | left(90) 5 | forward((i + 1) * 10) 6 | left(90) 7 | penup() 8 | forward(25) 9 | pendown() -------------------------------------------------------------------------------- /Plants.py: -------------------------------------------------------------------------------- 1 | needs_water = True 2 | needs_to_be_repotted = False 3 | 4 | print "Needs water: " + str(needs_water) 5 | print "Needs to be repotted: " + str(needs_to_be_repotted) -------------------------------------------------------------------------------- /Positive,Negative,Zero.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program determines if a number is positive, negative, or and then communicates 3 | that to the user by drawing a plus sign, minus sign, or 0. 4 | """ 5 | 6 | speed(5) 7 | 8 | # This function draws a zero in the middle of the canvas 9 | def draw_zero(): 10 | penup() 11 | setposition(0,-150) 12 | pendown() 13 | circle(50) 14 | 15 | # This function draws a minus sign in the middle of the canvas 16 | def draw_minus(): 17 | forward(50) 18 | backward(100) 19 | forward(50) 20 | 21 | # This function draws a plus sign in the middle of the canvas 22 | def draw_plus(): 23 | for i in range(4): 24 | forward(50) 25 | backward(50) 26 | left(90) 27 | 28 | # The user is asked to give a number. If they answer with a positive value, 29 | # a plus sign appears. If they answer with a negative value, a minus sign 30 | # appears and if they answer with a 0, a 0 appears. 31 | number = int(input("Enter a number: ")) 32 | 33 | pensize(10) 34 | color("blue") 35 | if number > 0: 36 | draw_plus() 37 | elif number < 0: 38 | draw_minus() 39 | else: 40 | draw_zero() -------------------------------------------------------------------------------- /Positive,Zero,orNegative?.py: -------------------------------------------------------------------------------- 1 | number = float(input("Please enter a number: ")) 2 | 3 | if number < 0: 4 | print("Negative") 5 | elif number > 0: 6 | print("Positive") 7 | else: 8 | print("Zero") -------------------------------------------------------------------------------- /PresidentialEligibility-Extended.py: -------------------------------------------------------------------------------- 1 | #Prompt for user's input and store it inside of variables 2 | 3 | age = int(input("Enter your age: ")) 4 | nationality = input("Enter if you from the US 'Yes' or 'No': ") 5 | resident = int(input("Enter how many years have you lived in the US: ")) 6 | 7 | #Check that requirements to run for presient have been met 8 | 9 | if (age >= 18 and nationality == "Yes" and resident >= 14): 10 | 11 | print("\nYou are eligible to run for president. Reasons for eligibility:") 12 | print("\b\tAge > 18\n\b\tUS naturally born citizen\n\b\tUs resident more than 14 years") 13 | 14 | else: #requirements were not met 15 | 16 | #Check to see which requirements were not met and report to the user 17 | 18 | if(age < 18 and nationality != "Yes" and resident < 14): #All three requirements have not been met? 19 | 20 | print("\nYou are not eligible to run for president Reasons for ineligibility:") 21 | print("\b\tYou are too young to run for president\n\b\tYou were not born in the United States\n\b\tYou have not been a United States resident for more than 14 years") 22 | 23 | else: #Only 1 or 2 requirement(s) have not been met 24 | 25 | #Check if two requirements were missed 26 | 27 | if(age < 18 and nationality != "Yes"): #First two requirements have not been met? 28 | 29 | print("\nYou are not eligible to run for president Reasons for ineligibility:") 30 | print("\b\tYou are too young to run for president\n\b\tYou were not born in the United States") 31 | 32 | elif(nationality != "Yes" and resident < 14): #Last two requirements have not been met? 33 | 34 | print("\nYou are not eligible to run for president Reasons for ineligibility:") 35 | print("\b\tYou were not born in the United States\n\b\tYou have not been a United States resident for more than 14 years") 36 | 37 | elif(resident < 14 and age < 18): #First and last requirement have not been met? 38 | 39 | print("\nYou are not eligible to run for president Reasons for ineligibility:") 40 | print("\b\tYou are too young to run for president\n\b\tYou have not been a United States resident for more than 14 years") 41 | 42 | else: #Only one requirement has been missed 43 | 44 | if(age < 18): 45 | 46 | print("\nYou are not eligible to run for president Reasons for ineligibility:") 47 | print("\b\tYou are too young to run for president") 48 | 49 | elif(nationality != "Yes"): 50 | 51 | print("\nYou are not eligible to run for president Reasons for ineligibility:") 52 | print("\b\tYou were not born in the United States") 53 | 54 | else: #There are no other alternitaves, the requirement missed must be residency 55 | 56 | print("\nYou are not eligible to run for president Reasons for ineligibility:") 57 | print("\b\tYou have not been a United States resident for more than 14 years") -------------------------------------------------------------------------------- /PresidentialEligibility.py: -------------------------------------------------------------------------------- 1 | age = int(input("Please enter your age: ")) 2 | nationality = input("Are you from the US 'Yes' or 'No': ") 3 | 4 | 5 | if (age >= 18 and nationality == "Yes"): 6 | print("You are eligible to run for president.") 7 | else: 8 | print("You are not eligible to run for president.") -------------------------------------------------------------------------------- /Print10Numbers.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program prints ten numbers - 0 through 9. 3 | """ 4 | 5 | # Inside the for loop, we can treat i like a normal integer variable. 6 | 7 | for i in range(10): 8 | print i -------------------------------------------------------------------------------- /PrintMultipleTimes.py: -------------------------------------------------------------------------------- 1 | def String(string, prints): 2 | ans = string * prints 3 | print ans 4 | 5 | String("Yo to the yo-yo! ", 12) -------------------------------------------------------------------------------- /PrintProduct.py: -------------------------------------------------------------------------------- 1 | def Mult(num1, num2): 2 | ans = num1 * num2 3 | print ans 4 | 5 | Mult(1, 5) -------------------------------------------------------------------------------- /PrintaNumber.py: -------------------------------------------------------------------------------- 1 | # This function takes a single parameter and prints it. 2 | 3 | def print_number(x): 4 | print x 5 | 6 | print_number(12) 7 | print_number("dog") -------------------------------------------------------------------------------- /PrintingMultipleLines.py: -------------------------------------------------------------------------------- 1 | print "This is a line of text!" 2 | print "Here's another line." -------------------------------------------------------------------------------- /ProgramTracing,Part2.py: -------------------------------------------------------------------------------- 1 | # Can you guess what this program prints? 2 | x = 5 3 | y = 5 4 | 5 | while x > 5 or y < 10: 6 | print x 7 | print y 8 | 9 | x = 15 - x 10 | y = y + 2 -------------------------------------------------------------------------------- /ProgramTracing.py: -------------------------------------------------------------------------------- 1 | # See if you can guess what this program prints! 2 | x = 10 3 | 4 | while x > 5: 5 | print x 6 | x = x - 2 -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CodeHS 2 | A collection of my CodeHS work from virtual school in 2021. My school account will be deleted, so it made sense to post it here. 3 | -------------------------------------------------------------------------------- /RainbowOctagon.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program will draw an octagon where each side is a different color. 3 | """ 4 | 5 | speed(5) 6 | pensize(5) 7 | 8 | # Draw one side of shape, then change color and turn 60 degrees before 9 | # drawing next line. Continue for 8 lines to complete hexagon. 10 | color("red") 11 | forward(50) 12 | left(45) 13 | color("orange") 14 | forward(50) 15 | left(45) 16 | color("yellow") 17 | forward(50) 18 | left(45) 19 | color("green") 20 | forward(50) 21 | left(45) 22 | color("blue") 23 | forward(50) 24 | left(45) 25 | color("indigo") 26 | forward(50) 27 | left(45) 28 | color("purple") 29 | forward(50) 30 | left(45) 31 | color("pink") 32 | forward(50) -------------------------------------------------------------------------------- /Rating.py: -------------------------------------------------------------------------------- 1 | 2 | rating = (int(input())) 3 | 4 | def draw(): 5 | right(180) 6 | color("green") 7 | left(45) 8 | forward(100) 9 | right(90) 10 | forward(50) 11 | 12 | if (rating == 9): 13 | draw() 14 | elif (rating >= 6): 15 | color("yellow") 16 | pensize(5) 17 | forward(50) 18 | 19 | elif (rating >= 1): 20 | left(45) 21 | pensize(5) 22 | color("red") 23 | forward(50) 24 | backward(100) 25 | forward(50) 26 | left(90) 27 | forward(50) 28 | backward(100) 29 | else: 30 | print("HUH?") -------------------------------------------------------------------------------- /Recipe.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program asks the user for three ingredients, 3 | three amounts, and a number of servings, and 4 | determines how much of each ingredient is needed 5 | to serve the specified number of servings. 6 | 7 | The names of three ingredients for a salad recipe. 8 | The number of ounces of each ingredient required for 9 | one serving (these should just be floats, like 2.5). 10 | The number of servings desired (this should be an 11 | integer). You should only ask for the number of 12 | servings once. 13 | """ 14 | 15 | ingredient_one = "" 16 | ingredient_two = "" 17 | ingredient_three = "" 18 | 19 | ingredient_one_amount = 0 20 | ingredient_two_amount = 0 21 | ingredient_three_amount = 0 22 | 23 | for i in range(3): 24 | 25 | x = input("What is the name of ingredient number " + str(i + 1) + "? ") 26 | y = float(input("What is the amount of ingredient " + str(i + 1) + " (oz)? ")) 27 | 28 | if i == 0: 29 | ingredient_one = x 30 | ingredient_one_amount = y 31 | print("0") 32 | elif i == 1: 33 | ingredient_two = x 34 | ingredient_two_amount = y 35 | print("1") 36 | else: 37 | ingredient_three = x 38 | ingredient_three_amount = y 39 | print("2") 40 | 41 | s = int(input("How many servings? ")) 42 | 43 | tot1 = ingredient_one_amount * s 44 | tot2 = ingredient_two_amount * s 45 | tot3 = ingredient_three_amount * s 46 | 47 | print("You will need:") 48 | print(str(tot1) + " times the amount of " + ingredient_one) 49 | print(str(tot2) + " times the amount of " + ingredient_two) 50 | print(str(tot3) + " times the amount of " + ingredient_three) -------------------------------------------------------------------------------- /Rectangle (1).py: -------------------------------------------------------------------------------- 1 | length = 10 2 | width = 5 3 | 4 | area = length * width 5 | perimeter = length * 2 + width * 2 6 | print(area) 7 | print(perimeter) -------------------------------------------------------------------------------- /Rectangle,Part2.py: -------------------------------------------------------------------------------- 1 | length = 10 2 | width = 5 3 | 4 | area = length * width 5 | perimeter = length * 2 + width * 2 6 | 7 | print("Area = " + str(area)) 8 | print("perimeter = " + str(perimeter)) -------------------------------------------------------------------------------- /Rectangle,Part3.py: -------------------------------------------------------------------------------- 1 | length = int(input("What is the length? ")) 2 | width = int(input("What is the width? ")) 3 | 4 | area = length * width 5 | perimeter = length * 2 + width * 2 6 | 7 | print("Area = " + str(area)) 8 | print("perimeter = " + str(perimeter)) -------------------------------------------------------------------------------- /Rectangle.py: -------------------------------------------------------------------------------- 1 | # draw the bottom of the rectangle 2 | forward(50) 3 | left(90) 4 | 5 | # draw the right of the rectangle 6 | forward(100) 7 | left(90) 8 | 9 | # draw the top of the rectangle 10 | forward(50) 11 | left(90) 12 | 13 | # draw the left of the rectangle 14 | 15 | forward(100) 16 | left(90) -------------------------------------------------------------------------------- /Return10.py: -------------------------------------------------------------------------------- 1 | # This function returns the number 10. Wherever it is used, Python 2 | # effectively replaces it with the number 10, similar to how Python 3 | # replaces 3 + 4 with the number 7. 4 | def return_ten(): 5 | return 10 6 | 7 | print return_ten() 8 | 9 | x = return_ten() 10 | print x + 1 -------------------------------------------------------------------------------- /RollerCoaster.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program determines if a rider reaches the height requirement for the 3 | roller coaster. 4 | """ 5 | 6 | height = 5.5 7 | height_requirement = 5.0 8 | 9 | # You must be at least 5.0 feet tall to ride the roller coaster. 10 | can_ride_roller_coaster = height >= height_requirement 11 | 12 | if can_ride_roller_coaster: 13 | print "You are tall enough to ride this roller coaster!" 14 | else: 15 | print "Sorry, you aren't tall enough." -------------------------------------------------------------------------------- /RollingDice.py: -------------------------------------------------------------------------------- 1 | for index1 in range(6): 2 | for index2 in range(6): 3 | print str(index1 + 1) + ", " + str(index2 + 1) -------------------------------------------------------------------------------- /RoomNames.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program asks the user for the building name and room number then outputs 3 | an abbreviation that identifies the room by taking the first two letters of the 4 | building name, and concatenating it with the room number. 5 | """ 6 | 7 | building_name = input("Building name: ") 8 | 9 | room_number = input("Room number: ") 10 | 11 | abbreviation = building_name[:2] + room_number 12 | print abbreviation -------------------------------------------------------------------------------- /RoundingError.py: -------------------------------------------------------------------------------- 1 | x = 0.0037 / 100 2 | y = 0.000037 3 | 4 | # These numbers look equal! 5 | print x 6 | print y 7 | 8 | # ... But they aren't exactly equal! 9 | if x == y: 10 | print "Equal!" 11 | else: 12 | print "Unequal!" 13 | 14 | # ... But they are approximately equal (to five decimal places)! 15 | if round(x, 5) == round(y, 5): 16 | print "Approximately equal!" 17 | else: 18 | print "Not approximately equal!" -------------------------------------------------------------------------------- /RowofCircles.py: -------------------------------------------------------------------------------- 1 | penup() 2 | backward(200) 3 | forward(10) 4 | for i in range(20): 5 | pendown() 6 | circle(10) 7 | penup() 8 | forward(20) -------------------------------------------------------------------------------- /RunningTotal,Part2.py: -------------------------------------------------------------------------------- 1 | total = 0 2 | number_of_numbers = int(input("How many numbers would you like to sum? ")) 3 | 4 | for i in range(number_of_numbers): 5 | next = int(input("Enter a number: ")) 6 | total = total + next 7 | 8 | print("Sum: " + str(total)) -------------------------------------------------------------------------------- /RunningTotal.py: -------------------------------------------------------------------------------- 1 | total = 0 2 | 3 | for i in range(10): 4 | next = int(input("Enter a number: ")) 5 | total = total + next 6 | 7 | print "Sum: " + str(total) -------------------------------------------------------------------------------- /SadFace.py: -------------------------------------------------------------------------------- 1 | def happy_face(): 2 | left(90) 3 | forward(100) 4 | pendown() 5 | pensize(5) 6 | circle(40, 180) 7 | 8 | def sad_face(): 9 | left(90) 10 | forward(100) 11 | pensize(5) 12 | circle(40, 180) 13 | pendown() 14 | circle(40, 180) 15 | 16 | def draw_face(): 17 | speed(10) 18 | 19 | penup() 20 | left(90) 21 | backward(100) 22 | right(90) 23 | color("yellow") 24 | pendown() 25 | begin_fill() 26 | circle(100) 27 | end_fill() 28 | penup() 29 | left(90) 30 | color("black") 31 | forward(155) 32 | left(90) 33 | backward(40) 34 | pendown() 35 | begin_fill() 36 | circle(10) 37 | end_fill() 38 | penup() 39 | forward(80) 40 | pendown() 41 | begin_fill() 42 | circle(10) 43 | end_fill() 44 | penup() 45 | 46 | 47 | draw_face() 48 | happy = input("Are you happy? Please enter 'YES' or 'NO': ") 49 | 50 | if happy == "YES": 51 | happy_face() 52 | else: 53 | sad_face() -------------------------------------------------------------------------------- /ShapeStack.py: -------------------------------------------------------------------------------- 1 | #Instructions 2 | #End Instructions 3 | 4 | w = 50 5 | h = 50 6 | 7 | #Early Setup 8 | speed(0) 9 | pensize(2) 10 | bgcolor("black") 11 | color("white") 12 | penup() 13 | setposition(-w / 2, -200) 14 | pendown() 15 | left(90) 16 | 17 | def draw_square(dist): 18 | for i in range(4): 19 | forward(dist) 20 | right(90) 21 | 22 | def draw_circle(rad): 23 | circle(rad) 24 | penup() 25 | backward(rad) 26 | left(90) 27 | forward(h) 28 | pendown() 29 | 30 | def next_shape_circle_prep(): 31 | right(90) 32 | forward(w / 2) 33 | left(90) 34 | penup() 35 | forward(h) 36 | pendown() 37 | right(90) 38 | 39 | for x in range(4): 40 | draw_square(h) 41 | next_shape_circle_prep() 42 | draw_circle(h / 2) -------------------------------------------------------------------------------- /ShorterDashedLine.py: -------------------------------------------------------------------------------- 1 | penup() 2 | backward(200) 3 | 4 | pendown(); 5 | forward(50); 6 | 7 | penup(); 8 | forward(50) 9 | 10 | pendown(); 11 | forward(50) 12 | 13 | penup(); 14 | forward(50) 15 | 16 | pendown(); 17 | forward(50) 18 | 19 | penup(); 20 | forward(50) 21 | 22 | pendown(); 23 | forward(50) 24 | 25 | penup(); 26 | forward(50) -------------------------------------------------------------------------------- /Sidewalk.py: -------------------------------------------------------------------------------- 1 | penup() 2 | speed(10) 3 | 4 | dist = 35 5 | 6 | setposition(-200 + dist, -200) 7 | 8 | def draw_squares(): 9 | for i in range(8): 10 | pendown() 11 | left(45) 12 | circle(dist - 5, 360, 4) 13 | right(45) 14 | penup() 15 | forward(dist + 10) 16 | 17 | for i in range(4): 18 | draw_squares() 19 | left(90) 20 | forward(dist + 7) -------------------------------------------------------------------------------- /Slinky.py: -------------------------------------------------------------------------------- 1 | circle(35) 2 | forward(20) 3 | circle(35) 4 | forward(20) 5 | circle(35) 6 | forward(20) 7 | circle(35) 8 | forward(20) 9 | circle(35) 10 | forward(20) -------------------------------------------------------------------------------- /Slopes.py: -------------------------------------------------------------------------------- 1 | coords, NUMCOORDS = [], 5 2 | 3 | for i in range(NUMCOORDS): #For each coord... 4 | while(True): #Always do the following 5 | try: coords.append(tuple((int(input(str(i + 1) + '. Enter \'x\'>>>')), int(input(str(i + 1) + '. Enter \'y\'>>>'))))) #Get and convert input to tuple 6 | except ValueError: continue #If exception occurs, try again! 7 | break #Otherwise, breakout of loop 8 | 9 | 10 | for k in range(len(coords) - 1): 11 | x1, x2 = coords[k][0], coords[k + 1][0] 12 | y1, y2 = coords[k][1], coords[k + 1][1] 13 | 14 | 15 | if(x1 - x2 == 0): 16 | print 'Undefined slope' 17 | else: 18 | result = float(y2 - y1) / (x2 - x1) 19 | print 'Slope between ' + str(coords[k]) + ' and ' + str(coords[k + 1]) + ' is ' + "{:.2f}".format(result) -------------------------------------------------------------------------------- /Snowman.py: -------------------------------------------------------------------------------- 1 | speed(10) 2 | left(90) 3 | penup() 4 | backward(200) 5 | right(90) 6 | 7 | radius = int(input("Enter a scale factor: ")) 8 | 9 | def draw(): 10 | 11 | color("gray") 12 | begin_fill() 13 | circle(radius) 14 | end_fill() 15 | left(90) 16 | forward(radius * 2) 17 | right(90) 18 | begin_fill() 19 | circle(radius / 2) 20 | end_fill() 21 | left(90) 22 | forward(radius) 23 | right(90) 24 | begin_fill() 25 | circle(radius / 4) 26 | end_fill() 27 | 28 | draw() -------------------------------------------------------------------------------- /SpellingBee.py: -------------------------------------------------------------------------------- 1 | word = "eggplant" 2 | 3 | # Give the word to spell 4 | print "Your word is: " + word + "." 5 | 6 | # Spell the word with a for loop here! 7 | for letter in word: 8 | print letter + "!" -------------------------------------------------------------------------------- /SplittingaString.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program will turn a phrase into a list by separating elements at 3 | the whitespace (or spaces) entered. 4 | """ 5 | 6 | my_string = "try to be a rainbow in someone's cloud" 7 | print my_string 8 | 9 | my_list = my_string.split() 10 | print my_list -------------------------------------------------------------------------------- /Square.py: -------------------------------------------------------------------------------- 1 | forward(50) 2 | left(90) 3 | forward(50) 4 | left(90) 5 | forward(50) 6 | left(90) 7 | forward(50) -------------------------------------------------------------------------------- /SquareSwirl.py: -------------------------------------------------------------------------------- 1 | """ 2 | This code will draw a square swirl from the center of the canvas. 3 | """ 4 | 5 | speed(5) 6 | 7 | # Move forward and turn left 10 times. Move 10 pixels further every iteration 8 | for i in range(10,101,10): 9 | forward(i) 10 | left(90) -------------------------------------------------------------------------------- /SquareUsingLoops.py: -------------------------------------------------------------------------------- 1 | for i in range(4): 2 | forward(50) 3 | left(90) -------------------------------------------------------------------------------- /StretchedSlinky.py: -------------------------------------------------------------------------------- 1 | #this is a single line coment 2 | """ 3 | This is a 4 | multiline coment 5 | #single line coments are concidered multiline coments within 6 | """ 7 | #""" multiline coments are concidered single line coments """ 8 | 9 | circle(35) 10 | forward(40) 11 | 12 | circle(35) 13 | forward(40) 14 | 15 | circle(35) 16 | forward(40) 17 | 18 | circle(35) 19 | forward(40) 20 | 21 | circle(35) 22 | forward(40) -------------------------------------------------------------------------------- /String<-->List.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program shows how strings can be turned into lists and lsits into strings. 3 | """ 4 | 5 | my_string = "abcde" 6 | print my_string 7 | 8 | # Turn string into a list 9 | my_list = list(my_string) 10 | print my_list 11 | 12 | # Turn list into a string 13 | my_other_string = "".join(my_list) 14 | print my_other_string -------------------------------------------------------------------------------- /StringCollage.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program gives several examples of creating strings out of two other 3 | strings, or out of pieces of those strings. 4 | """ 5 | 6 | string_one = "abcde" 7 | string_two = "fghij" 8 | 9 | print "string1: " + string_one 10 | print "string2: " + string_two 11 | 12 | string_three = string_one + string_two 13 | print "string3: " + string_three 14 | 15 | string_four = string_two + string_one 16 | print "string4: " + string_four 17 | 18 | string_five = string_one + " " + string_two 19 | print "string5: " + string_five 20 | 21 | string_six = string_one[0] + " " + string_two[1:] 22 | print "string6: " + string_six -------------------------------------------------------------------------------- /StringForLoopwithCharacters.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program uses a for loop to print out each letter in the string on its 3 | own line. 4 | """ 5 | 6 | my_string = "hello" 7 | for letter in my_string: 8 | print letter -------------------------------------------------------------------------------- /StringForLoopwithIndices.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program uses a for loop to print out each letter in the string on its 3 | own line using indicies. 4 | """ 5 | 6 | my_string = "hello" 7 | for i in range(len(my_string)): 8 | print my_string[i] -------------------------------------------------------------------------------- /StringImmutability.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program shows the immutability of strings. 3 | """ 4 | 5 | my_string = "hello!" 6 | 7 | my_string = "hi!" 8 | 9 | my_other_string = my_string[:2] 10 | 11 | # This line will cause an error because it tries to change a string. 12 | my_string[0] = "H" -------------------------------------------------------------------------------- /StringsToIntegers.py: -------------------------------------------------------------------------------- 1 | def safe_int(char): 2 | try: 3 | return int(char) 4 | except: 5 | return 0 6 | 7 | list_of_strings = ["a", "2", "7", "zebra"] 8 | final_list = [] 9 | 10 | for char in [x for x in list_of_strings]: final_list.append(safe_int(char)) 11 | 12 | print final_list -------------------------------------------------------------------------------- /SumTwoNumbers.py: -------------------------------------------------------------------------------- 1 | def add(num1, num2): 2 | return num1 + num2 3 | 4 | print add(1, 2) -------------------------------------------------------------------------------- /Swapping.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program shows how to swap variables using unpacking. 3 | """ 4 | 5 | first = 10 6 | second = 20 7 | 8 | print "Before swap: first is " + str(first) + ", second is " + str(second) 9 | 10 | # Swap using unpacking 11 | first, second = second, first 12 | 13 | print "After swap: first is " + str(first) + ", second is " + str(second) -------------------------------------------------------------------------------- /TableReservation.py: -------------------------------------------------------------------------------- 1 | reservation_name = "Bob" 2 | 3 | name = input("Please enter your name: ") 4 | 5 | if reservation_name == name: 6 | print "Right this way!" 7 | else: 8 | print "Sorry, we don't have a reservation under that name." -------------------------------------------------------------------------------- /TeamSizes.py: -------------------------------------------------------------------------------- 1 | num_people = int(input("How many people are playing?: ")) 2 | num_teams = int(input("How many teams?: ")) 3 | 4 | people_per_team = num_people/ num_teams 5 | left_over = num_people % num_teams 6 | 7 | print "If there are " + str(num_teams) + " teams, there will be " + \ 8 | str(people_per_team) + " on each team, with " + str(left_over) + " left over." -------------------------------------------------------------------------------- /TemperatureConverter,Part2.py: -------------------------------------------------------------------------------- 1 | # Write your function for converting Celsius to Fahrenheit here. 2 | # Make sure to include a comment at the top that says what 3 | # each function does! 4 | 5 | 6 | def F2C(temp): 7 | try: 8 | return (1.8 * temp) + 32 9 | except: 10 | print "ValueError" 11 | def C2F(temp): 12 | try: 13 | return (temp - 32) / 1.8 14 | except: 15 | print "ValueError" 16 | 17 | # Now write your function for converting Fahrenheit to Celsius. 18 | 19 | 20 | 21 | 22 | # Now change 0C to F: 23 | print C2F("farglebartgle") 24 | 25 | # Change 100C to F: 26 | print C2F(100) 27 | 28 | # Change 40F to C: 29 | print F2C(30) 30 | 31 | # Change 80F to C: 32 | print F2C(80) -------------------------------------------------------------------------------- /TemperatureConverter.py: -------------------------------------------------------------------------------- 1 | # Write your function for converting Celsius to Fahrenheit here. 2 | # Make sure to include a comment at the top that says what 3 | # each function does! 4 | 5 | 6 | def F2C(temp): 7 | return (1.8 * temp) + 32 8 | 9 | def C2F(temp): 10 | return (temp - 32) / 1.8 11 | 12 | 13 | # Now write your function for converting Fahrenheit to Celsius. 14 | 15 | 16 | 17 | 18 | # Now change 0C to F: 19 | print C2F(0) 20 | 21 | # Change 100C to F: 22 | print C2F(100) 23 | 24 | # Change 40F to C: 25 | print F2C(30) 26 | 27 | # Change 80F to C: 28 | print F2C(80) -------------------------------------------------------------------------------- /TheinKeyword.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program shows how the in keyword can be used to determine whether or 3 | not a particular key exists in a dictionary. 4 | """ 5 | 6 | my_dictionary = { 7 | "a": 1, 8 | "b": 2 9 | } 10 | 11 | # This key is in my_dictionary, so this will print True. 12 | print "a" in my_dictionary 13 | 14 | # This one isn't, so this will print False. 15 | print "z" in my_dictionary 16 | 17 | # 2 is not a key in my dictionary, so this will print False. 18 | print 2 in my_dictionary 19 | 20 | # You can also write a for loop that iterates 21 | # over just the keys in the dictionary. 22 | for key in my_dictionary: 23 | print "key: " + str(key) 24 | print "value: " + str(my_dictionary[key]) -------------------------------------------------------------------------------- /ThelenFunction.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program shows how to use the len function to determine the length of 3 | a string. 4 | """ 5 | 6 | print len("hello") 7 | print len("") 8 | print len("a b c d e") 9 | 10 | my_string = "hello" 11 | bad_index = len(my_string) 12 | 13 | # This line causes an error. 14 | print my_string[bad_index] -------------------------------------------------------------------------------- /TicTacToe.py: -------------------------------------------------------------------------------- 1 | def get_valid_index(prompt): 2 | while True: 3 | try: 4 | index = int(input(prompt)) 5 | if index >= 0 and index <= 2: 6 | return index 7 | print "Must be 0 - 2 inclusive!" 8 | except ValueError: 9 | print "Must be an integer!" 10 | def game_is_over(board): 11 | for i in range(3): 12 | # Check horizontal 13 | if board[i][0] == board[i][1] == board[i][2] \ 14 | and board[i][0] != " ": 15 | print board[i][0] + " wins!" 16 | return True 17 | 18 | # Check vertical 19 | if board[0][i] == board[1][i] == board[2][i] \ 20 | and board[0][i] != " ": 21 | print board[0][i] + " wins!" 22 | return True 23 | 24 | # Check diagonals 25 | if board[0][0] == board[1][1] == board[2][2] \ 26 | and board[0][0] != " ": 27 | print board[0][0] + " wins!" 28 | return True 29 | 30 | if board[2][0] == board[1][1] == board[0][2] \ 31 | and board[2][0] != " ": 32 | print board[2][0] + " wins!" 33 | return True 34 | 35 | # Check tie 36 | if " " not in board[0] and " " not in board[1] \ 37 | and " " not in board[2]: 38 | print "Tie game!" 39 | return True 40 | 41 | # Not over yet! 42 | return False 43 | def print_board(board): 44 | 45 | for i in range(len(board)): 46 | [str(x) for x in board[i]] 47 | print " ".join([str(x) for x in board[i]]) 48 | 49 | # TODO: Set up the board as a 3x3 grid of spaces here... 50 | board = [ [' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' '] ] 51 | 52 | print_board(board) 53 | 54 | # x goes first 55 | 56 | 57 | # Play tic tac toe 58 | turnCounter = 0 59 | while not game_is_over(board): 60 | 61 | if(turnCounter % 2 == 0): 62 | turn = 'X' 63 | else: 64 | turn = 'O' 65 | 66 | print "It's " + turn + "'s turn." 67 | row = get_valid_index("Row: ") 68 | col = get_valid_index("Col: ") 69 | 70 | board[row][col] = turn 71 | print_board(board) 72 | 73 | turnCounter = turnCounter + 1 -------------------------------------------------------------------------------- /Transaction.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program simulates a single transaction - 3 | either a deposit or a withdrawal - at a bank. 4 | """ 5 | 6 | money = 1000 7 | 8 | mode = input("deposit or withdrawl: ") 9 | 10 | if mode == "deposit": 11 | deposit_amount = int(input("Enter amount: ")) 12 | money = money + deposit_amount 13 | 14 | print("") 15 | print("final: " + str(money)) 16 | print("") 17 | 18 | elif mode == "withdrawl": 19 | withdraw_amount = int(input("Please enter the amount: ")) 20 | if withdraw_amount > 1000: 21 | print("You cannot have a negative balance!") 22 | else: 23 | money = money - withdraw_amount 24 | print("") 25 | print("final: " + str(money)) 26 | print("") 27 | else: 28 | print("Invalid Transaction!") -------------------------------------------------------------------------------- /Trapezoid.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program computes the area 3 | of a trapezoid. 4 | """ 5 | 6 | # Ask for the length of the two 7 | # parallel sides - base and top 8 | base = int(input("Base: ")) 9 | top = int(input("Top: ")) 10 | 11 | # Ask for the height 12 | height = int(input("Height: ")) 13 | 14 | # Compute and print the area of the 15 | # trapezoid. 16 | area = 0.5 * (base + top) * height 17 | print "Area: " + str(area) -------------------------------------------------------------------------------- /TuplesWithaSingleElement.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program shows how to create a tuple with a single element. 3 | """ 4 | 5 | # This just creates an integer... 6 | x = (3) 7 | print x 8 | print type(x) 9 | 10 | # This creates a tuple 11 | x = (3,) 12 | print x 13 | print type(x) -------------------------------------------------------------------------------- /TypeConversion,Part2.py: -------------------------------------------------------------------------------- 1 | x = int(input("Enter a number: ")) 2 | print type(x) 3 | print x 4 | print x + 3 -------------------------------------------------------------------------------- /TypeConversion.py: -------------------------------------------------------------------------------- 1 | x = input("Enter a number: ") 2 | print type(x) 3 | 4 | x = int(x) 5 | print type(x) 6 | 7 | print x + 3 8 | 9 | var = "Not a number" 10 | var = float(var) -------------------------------------------------------------------------------- /UndefinedVariables.py: -------------------------------------------------------------------------------- 1 | a = int(10) 2 | c = int(20) 3 | b = c 4 | d = a + b 5 | e = 80 6 | 7 | print a 8 | print b 9 | print c 10 | print d 11 | print e -------------------------------------------------------------------------------- /Unpacking.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program shows how unpacking can be used to separate out elements in 3 | a list or tuple. 4 | """ 5 | 6 | # Assign multiple variables at a time, like this: 7 | x, y, z = 1, 2, 3 8 | print "x: " + str(x) 9 | print "y: " + str(y) 10 | print "z: " + str(z) 11 | 12 | # Assign multiple variables to a list (only if the number of variables 13 | # is the SAME as the number of elements in the list.) 14 | my_list = [10, 20, 30] 15 | x, y, z = my_list 16 | print "x: " + str(x) 17 | print "y: " + str(y) 18 | print "z: " + str(z) 19 | 20 | # A tuple can also be used 21 | my_tuple = (1.0, 2.0, 3.0) 22 | x, y, z = my_tuple 23 | print "x: " + str(x) 24 | print "y: " + str(y) 25 | print "z: " + str(z) -------------------------------------------------------------------------------- /UnpackingParameters.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program shows how unpacking can be used to assign parameters to a function. 3 | """ 4 | 5 | def sum_three_numbers(x, y, z): 6 | return x + y + z 7 | 8 | my_list = [1, 2, 3] 9 | 10 | # Split list into respective pieces using asterisk 11 | sum = sum_three_numbers(*my_list) 12 | print sum -------------------------------------------------------------------------------- /VariableNaming.py: -------------------------------------------------------------------------------- 1 | name = "Tricia" 2 | print "Name" 3 | print "-----" 4 | print name 5 | print "-----" 6 | 7 | height_in_cm = 175 8 | print "Height in centimeters" 9 | print "-----" 10 | print height_in_cm 11 | print "-----" 12 | 13 | company = "Google" 14 | print "Company" 15 | print "-----" 16 | print company 17 | print "-----" 18 | 19 | favorite_cereal = "Raisin Bran" 20 | print "Favorite cereal" 21 | print "-----" 22 | print favorite_cereal 23 | print "-----" -------------------------------------------------------------------------------- /VariableTypes.py: -------------------------------------------------------------------------------- 1 | print type(2) 2 | print type("Hi") 3 | 4 | print "-----" 5 | 6 | pi = 3.14 7 | initial = "c" 8 | 9 | print type(pi) 10 | print type(initial) 11 | 12 | #MY STUFF 13 | 14 | print type(False) 15 | print type("YO!") 16 | print type(2.5) 17 | print type(1) 18 | 19 | while(pi == 3.14): 20 | #The input method is always a string 21 | print type(input("Enter Something: ")) -------------------------------------------------------------------------------- /VariablesinAction.py: -------------------------------------------------------------------------------- 1 | greeting = "Hi there!" 2 | num = 3 3 | value = float(num) 4 | 5 | print greeting 6 | print num 7 | print value -------------------------------------------------------------------------------- /Verticalname.py: -------------------------------------------------------------------------------- 1 | print "I" 2 | print "B" 3 | print "X" 4 | print "C" 5 | print "O" 6 | print "D" 7 | print "E" 8 | print "C" 9 | print "A" 10 | print "T" -------------------------------------------------------------------------------- /VowelsinFirstName.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | def calc(): 4 | printString = "" 5 | index = 0 6 | 7 | 8 | for letter in name.upper(): 9 | if(letter.find("A") > -1 or letter.find("E") > -1 or letter.find("I") > -1 or letter.find("O") > -1 or letter.find("U") > -1): 10 | printString = printString + "\n There is an " + letter.lower() + " in your name, first found at index " + str(index) + "" 11 | 12 | index = index + 1 13 | 14 | return printString 15 | 16 | name = input("Please enter your first name: ") 17 | 18 | print "" 19 | 20 | print calc() -------------------------------------------------------------------------------- /Weather.py: -------------------------------------------------------------------------------- 1 | def sunny(): 2 | print "On a sunny day, sandals are appropriate footware." 3 | 4 | def rainy(): 5 | print "On a rainy day, galoshes are appropriate footware." 6 | 7 | def snowy(): 8 | print "On a snowy day, boots are appropriate footware." 9 | 10 | #====================MAIN CODE============================== 11 | 12 | weather = input("Enter the weather (sunny, rainy snowy): ") 13 | 14 | if(weather != "sunny" and weather != "rainy" and weather != "snowy"): 15 | print "\n" + str(weather) + " is not a valid response. :(" 16 | else: 17 | if(weather == "sunny"): 18 | sunny() 19 | elif(weather == "rainy"): 20 | rainy() 21 | else: 22 | snowy() -------------------------------------------------------------------------------- /WordLadder.py: -------------------------------------------------------------------------------- 1 | BREAK = -1 2 | 3 | def Init(): 4 | return input("Enter an initial word for the word ladder\n") 5 | 6 | def NextChar(): 7 | 8 | returnValue = input("Enter a char: ") 9 | 10 | while(returnValue.isupper()): 11 | print "Character must be a lowercase letter!" 12 | 13 | returnValue = input("Enter a char: ") 14 | 15 | while(len(returnValue) > 1): 16 | print "Must be exactly one character!" 17 | 18 | returnValue = input("Enter a char: ") 19 | 20 | return returnValue 21 | 22 | def NextIndex(word): 23 | 24 | invalid = True 25 | 26 | while(invalid): 27 | try: 28 | returnValue = int(input("Enter an index to modify (-1 to quit): ")) 29 | 30 | if(returnValue < -1 or returnValue > len(word)): 31 | print "Invalid index" 32 | else: 33 | invalid = False 34 | return returnValue 35 | 36 | except ValueError: 37 | print "ValueErr: Index must be of type 'int'." 38 | 39 | #==============================================================================# 40 | 41 | initState = Init() 42 | 43 | _init = initState 44 | _index = NextIndex(_init) 45 | 46 | while(_index != BREAK): 47 | _init = _init[0 : _index] + NextChar() + _init[_index + 1 : len(_init)] 48 | print _init 49 | 50 | _index = NextIndex(_init) -------------------------------------------------------------------------------- /XandYAxes.py: -------------------------------------------------------------------------------- 1 | penup() 2 | left(90) 3 | forward(200) 4 | right(90) 5 | right(90) 6 | pendown() 7 | forward(400) 8 | penup() 9 | right(90) 10 | forward(200) 11 | right(90) 12 | forward(200) 13 | right(90) 14 | pendown() 15 | forward(400) -------------------------------------------------------------------------------- /XandYAxeswithHashMarks.py: -------------------------------------------------------------------------------- 1 | """ 2 | This code will have Tracy draw an x and y-axis on our screen with 3 | hash marks every 25 pixels. 4 | """ 5 | 6 | speed(5) 7 | 8 | # This function will draw a line with hash marks at every 25 pixels. 9 | def draw_hashed_axis(): 10 | pendown() 11 | for i in range(16): 12 | forward(25) 13 | right(90) 14 | forward(10) 15 | backward(20) 16 | forward(10) 17 | left(90) 18 | 19 | # Move Tracy to top of canvas and call draw hash marks function for y-axis. 20 | # Then move Tracy to left of canvas and call hash marks function for x-axis. 21 | penup() 22 | setposition(0,200) 23 | right(90) 24 | draw_hashed_axis() 25 | penup() 26 | setposition(-200,0) 27 | left(90) 28 | draw_hashed_axis() -------------------------------------------------------------------------------- /XandYAxiswithBoldedMarks.py: -------------------------------------------------------------------------------- 1 | """ 2 | This code will have Tracy draw an x and y-axis on our screen. Every other hash 3 | mark will be bolded. 4 | """ 5 | 6 | speed(5) 7 | 8 | # This function will draw a line with hash marks at every 25 pixels. The variable 9 | # 'count' increases each time the loop iterates and changes the thickness of the pen 10 | # based on the count value. 11 | def hash_marks(): 12 | count = 0 13 | for i in range(16): 14 | pendown() 15 | forward(25) 16 | right(90) 17 | if count % 2 == 0: 18 | pensize(5) 19 | forward(10) 20 | backward(20) 21 | forward(10) 22 | pensize(1) 23 | left(90) 24 | penup() 25 | count = count + 1 26 | 27 | # Move Tracy to starting position and then call hash marks function 28 | penup() 29 | setposition(0,200) 30 | right(90) 31 | hash_marks() 32 | setposition(-200,0) 33 | left(90) 34 | hash_marks() -------------------------------------------------------------------------------- /YourFirstIfStatement.py: -------------------------------------------------------------------------------- 1 | has_dog = True 2 | 3 | print "Do I have to walk the dog?" 4 | 5 | # The print on line 8 ONLY happens if 6 | # hasDog is True. 7 | if has_dog: 8 | print "Yup, I sure do!" -------------------------------------------------------------------------------- /appendandextend.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program demonstrates how append and extend can be used to add to a list. 3 | """ 4 | 5 | # Start with an empty list 6 | my_list = [] 7 | print my_list 8 | 9 | # Append things to the end of the list. 10 | # Each call to append changes the list. 11 | my_list.append(1) 12 | my_list.append(2) 13 | my_list.append(3) 14 | my_list.append(4) 15 | print my_list 16 | 17 | # Extend adds a list to a list! 18 | my_list.extend([5, 6, 7]) 19 | print my_list 20 | 21 | # This line will not actually change the value of the variable 'my_list' 22 | print my_list + [10, 12, 14] 23 | print my_list -------------------------------------------------------------------------------- /count.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program uses the count method which does not change the list. It returns 3 | an integer that represents how many instances of a particular thing were 4 | found in the list. 5 | """ 6 | 7 | # List of numbers 8 | my_list = [1, 4, 2, -4, 10, 0, 4, 2, 1, 4] 9 | print my_list 10 | 11 | # Find how many of each number are in the list 12 | print "There are " + str(my_list.count(4)) + " 4's in this list." 13 | print "There are " + str(my_list.count(1)) + " 1's in this list." 14 | print "There are " + str(my_list.count(-4)) + " -4's in this list." 15 | print "There are " + str(my_list.count(700)) + " 700's in this list." 16 | 17 | print "-----------------" 18 | 19 | # List of strings 20 | list_of_strings = ["hi", "hello", "hi", "hi", "hello"] 21 | print list_of_strings 22 | 23 | # Find how many of each word are in the list 24 | print "There are " + str(list_of_strings.count("hi")) + " hi's in this list." 25 | print "There are " + str(list_of_strings.count("hello")) + " hello's in this list." -------------------------------------------------------------------------------- /find.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program uses the find method which returns the first index at which 3 | a particular string is found within a string, or -1 if it is not found. 4 | """ 5 | 6 | s = "abra kadabra alakazam" 7 | print s 8 | print s.find("a") 9 | print s.find("kadabra") 10 | print s.find("q") -------------------------------------------------------------------------------- /remove.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program uses the remove method to remove a particular thing from a list. 3 | """ 4 | 5 | my_list = ["apple", "banana", "orange", "grapefruit"] 6 | print my_list 7 | 8 | my_list.remove("apple") 9 | print my_list 10 | 11 | # Note that it ONLY removes the first occurrence of something. 12 | 13 | my_list = ["apple", "banana", "orange", "grapefruit", "apple"] 14 | print my_list 15 | 16 | my_list.remove("apple") 17 | print my_list -------------------------------------------------------------------------------- /reverse.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program uses the reverse method to change a list such that it is in the 3 | reverse order that it was in. Note that it does NOT put it in reverse sorted 4 | order (unless the list was ALREADY in sorted order). 5 | """ 6 | 7 | my_list = [1, 4, 2, -4, 10, 0] 8 | print my_list 9 | 10 | my_list.reverse() 11 | print my_list -------------------------------------------------------------------------------- /sort.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program uses the sort method on a list to change the list such that the 3 | elements are placed in order from least to greatest. 4 | """ 5 | 6 | my_list = [1, 4, 2, -4, 10, 0] 7 | print my_list 8 | 9 | my_list.sort() 10 | print my_list -------------------------------------------------------------------------------- /strip.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program uses the strip method which returns a new string equal to the 3 | old string with leading and trailing whitespace characters (spaces, tabs, etc.) 4 | removed. 5 | """ 6 | 7 | s = " hi there " 8 | print s 9 | print s.strip() -------------------------------------------------------------------------------- /swapcase.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program uses the swapcase method which returns a new string equal to the 3 | old string, but with uppercase letters turned into lowercase letters 4 | and vise versa. 5 | """ 6 | 7 | s = "PyThOn" 8 | print s 9 | print s.swapcase() -------------------------------------------------------------------------------- /upperandlower.py: -------------------------------------------------------------------------------- 1 | """ 2 | This program turns a string into all capital or all lowercase letters. 3 | """ 4 | 5 | first_string = "hello" 6 | second_string = first_string.upper() 7 | third_string = second_string.lower() 8 | 9 | print first_string 10 | print second_string 11 | print third_string --------------------------------------------------------------------------------