# -*- coding: utf-8 -*- """ Created on Wed Feb 20 11:20:15 2019 Exercises to go over: Adding, removing and selecting an element For Each Loop Notes on Arrays:https://www.w3schools.com/python/python_lists.asp @author: achuang """ def pets(): """ Run this code. Then uncomment the last line and run it again. What do you notice? Your Task: Make the code print out 6 animals """ petType = ["dog","cat","fish"] print(petType) print(petType[1]) print(petType[0]) print(len(petType)) print(petType[90]) petType[1] = None print(petType) petType[1] = petType[2] petType[2] = None print(petType) def print_all_items(): """ Print all the items on a new line by using a while loop. First find the total amount, and then for each one, print it out. You will need to start your index at 0. """ items = ["bow","sword","staff","wand","shield","arrow","candle","bomb","hook","candle","bottle"] print(items) def print_all_items_for(): """ Print all the items on a new line by using a for loop """ items2 = ["bow","sword","staff","wand","shield","arrow","candle","bomb","hook","candle","bottle"] for x in items2: print(str(x)) def record_input(): """ Ask the user to enter 5 things of your choice. Add them all to an array and print them all out Try the .append() built in function """ itemsnum = 0 items3 = [] while (itemsnum < 5): print("please add a item") (items3.append(input(""))) itemsnum = itemsnum + 1 if(itemsnum == 5): print("here are your variables") for x in items3: print(str(x)) break def array_total(): """ Use a loop to find the sum of all the numbers in the array """ numbers = [8,3,5,7,2,6,9] sumnumb = 0 for x in numbers: sumnumb = x + sumnumb print(str(sumnumb)) def longer_than_five(): """ Use a loop to see how many of the words have more than 4 characters? How many less than 4? 4 Exactly? """ words = ["hello","purple","yes","blue","computer","laptop","fish","sun","hexahedron"] wordless = 0 wordmore = 0 for x in words: if (len(x) <= 4): wordless = wordless + 1 else: wordmore = wordmore +1 print("there are " + str(wordless) + " 4/bellow letter words and " + str(wordmore) + " 5/above letter words") def largest(): """ Use a loop to find the largest number """ numbers = [8,3,5,7,2,6,9] largest = numbers[0] for x in numbers: if (x >= float(largest)): largest = x print(str(largest)) def user_largest(): """ Have the user enter a number, say how many of the list has a number greater than the one user have. """ numbers = [8,3,5,7,2,6,9] larger = 0 print("pick a number") num = input() for x in numbers: if (x >= 4): larger = larger +1 print(str(larger) + " numbers are larger than " + str(num)) def second_largest(): """ Use a loop to find the second largest number """ numbers = [8,3,5,7,2,6,9] largest = numbers[0] prev_largest = numbers[0] for x in numbers: if (x >= float(largest)): prev_largest = largest largest = x print(str(prev_largest)) def full_names(): """ Have 2 arrays with corresponding values, first and last letter of their names. Print out the full initials of the following people Is there anything we should check before we start printing? What's easier, for loops or while loops? """ First_Initials = ["A","B","C","D","E","F","G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"] Second_Initials = ["A","B","C","D","E","F","G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"] Name_Valid = 0 First_Name_Valid = 0 Second_Name_Valid = 0 while(True): if(Name_Valid == 0): print("please give us you first name") User_First = input() print("please give us your second name") User_Second = input() User_First = User_First[0].upper() User_Second = User_Second[0].upper() for x in First_Initials: if(User_First == x): First_Name_Valid = 1 for x in Second_Initials: if(User_Second == x): Second_Name_Valid = 1 if(First_Name_Valid == 1 and Second_Name_Valid == 1): Name_Valid = 1 print("your initials are: " + str(User_First) + str(User_Second)) break def sort(): """ WITHOUT using any built in functions/libraries, sort this array unsorted = [3,5,2,7,6,8,99,4,0,2] unsorted.sort() for x in unsorted: print(str(x)) """ unsorted_array = [3,5,2,7,6,8,99,4,0,2] smallest = 0 sorted_array = [] for x in unsorted_array: for x in unsorted_array: if (x <= float(smallest)): smallest = x sorted_array.append(smallest) unsorted_array.pop(x) for x in sorted_array: print(str(x)) def reverse_sort(): """ WITHOUT using any built in functions/libraries, sort this array in an reverse order unsorted = [3,5,2,7,6,8,99,4,0,2] unsorted.sort(reverse = True) for x in unsorted: print(str(x)) """