Last active
March 30, 2024 20:13
-
-
Save LizaPiya/8df0a18c4ad952ee8a5c49c43fa6dc38 to your computer and use it in GitHub Desktop.
WEEK-4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## Create a function called mult that has two parameters, the first is required and should be an integer, the second is an optional parameter that can either be a number or a string but whose default is 6. The function should return the first parameter multiplied by the second. | |
| def mult(a,b=6): | |
| return a*b |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## def greeting(greeting="Hello ", name, excl="!"): | |
| return greeting + name + excl | |
| print(greeting("Bob")) | |
| print(greeting("")) | |
| print(greeting("Bob", excl="!!!")) | |
| Need to change- | |
| def greeting(name, greeting="Hello ",excl="!"): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## Below is a function, sum, that does not work. Change the function definition so the code works. The function should still have a required parameter, intx, and an optional parameter, intz with a defualt value of 5. | |
| def sum(intz=5, intx): | |
| return intz + intx | |
| def sum(intx,intz=5): | |
| return intz + intx |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Write a function, test, that takes in three parameters: a required integer, an optional boolean whose default value is True, and an optional dictionary, called dict1, whose default value is {2:3, 4:5, 6:8}. If the boolean parameter is True, the function should test to see if the integer is a key in the dictionary. The value of that key should then be returned. If the boolean parameter is False, return the boolean value “False”. | |
| def test(a,b=True,dict1={2:3, 4:5, 6:8}): | |
| if b == True: | |
| for k,v in dict1.items(): | |
| if a==k: | |
| return v | |
| return False | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## Write a function called checkingIfIn that takes three parameters. The first is a required parameter, which should be a string. The second is an optional parameter called direction with a default value of True. The third is an optional parameter called d that has a default value of {'apple': 2, 'pear': 1, 'fruit': 19, 'orange': 5, 'banana': 3, 'grapes': 2, 'watermelon': 7}. Write the function checkingIfIn so that when the second parameter is True, it checks to see if the first parameter is a key in the third parameter; if it is, return True, otherwise return False. | |
| But if the second paramter is False, then the function should check to see if the first parameter is not a key of the third. If it’s not, the function should return True in this case, and if it is, it should return False. | |
| def checkingIfIn(a,direction=True,d={'apple': 2, 'pear': 1, 'fruit': 19, 'orange': 5, 'banana': 3, 'grapes': 2, 'watermelon': 7}): | |
| if direction == True: | |
| if a in d: | |
| return True | |
| else: | |
| return False | |
| else: | |
| if a not in d: | |
| return True | |
| else: | |
| return False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## We have provided the function checkingIfIn such that if the first input parameter is in the third, dictionary, input parameter, then the function returns that value, and otherwise, it returns False. Follow the instructions in the active code window for specific variable assignmemts. | |
| def checkingIfIn(a, direction = True, d = {'apple': 2, 'pear': 1, 'fruit': 19, 'orange': 5, 'banana': 3, 'grapes': 2, 'watermelon': 7}): | |
| if direction == True: | |
| if a in d: | |
| return d[a] | |
| else: | |
| return False | |
| else: | |
| if a not in d: | |
| return True | |
| else: | |
| return d[a] | |
| # Call the function so that it returns False and assign that function call to the variable c_false | |
| c_false=checkingIfIn("mula") | |
| # Call the fucntion so that it returns True and assign it to the variable c_true | |
| c_true=checkingIfIn("coffee",direction = False) | |
| # Call the function so that the value of fruit is assigned to the variable fruit_ans | |
| fruit_ans=checkingIfIn("fruit",direction = True) | |
| # Call the function using the first and third parameter so that the value 8 is assigned to the variable param_check | |
| param_check= checkingIfIn("jaam", direction = True, d = {'jaam':8}) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## Write a function, sublist, that takes in a list of numbers as the parameter. In the function, use a while loop to return a sublist of the input list. The sublist should contain the same values of the original list up until it reaches the number 5 (it should not contain the number 5). | |
| def sublist(list): | |
| new_list=[] | |
| i=0 | |
| while i < len(list): | |
| if list[i] == 5: | |
| break | |
| new_list.append(list[i]) | |
| i=i+1 | |
| return new_list | |
| integer_list=[1,2,3,4,5] | |
| list=integer_list | |
| sublist(list) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## Write a function called check_nums that takes a list as its parameter, and contains a while loop that only stops once the element of the list is the number 7. What is returned is a list of all of the numbers up until it reaches 7. | |
| def check_nums(list): | |
| i=0 | |
| New_list=[] | |
| while i < len(list): | |
| if list[i] == 7: | |
| break | |
| New_list.append(list[i]) | |
| i=i+1 | |
| return New_list | |
| integer_list=[1,2,3,4,5,6,7,8,9] | |
| list=integer_list | |
| print (check_nums(list)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## Write a function, sublist, that takes in a list of strings as the parameter. In the function, use a while loop to return a sublist of the input list. The sublist should contain the same values of the original list up until it reaches the string “STOP” (it should not contain the string “STOP”). | |
| def sublist(list): | |
| i=0 | |
| New_list=[] | |
| while i < len(list): | |
| if list[i] == "STOP": | |
| break | |
| New_list.append(list[i]) | |
| i=i+1 | |
| return New_list | |
| New_string_list=["aam","jaam","kola","pepe","kathal"] | |
| list=New_string_list | |
| print (sublist(list)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ##Write a function called stop_at_z that iterates through a list of strings. Using a while loop, append each string to a new list until the string that appears is “z”. The function should return the new list. | |
| def stop_at_z(list): | |
| i=0 | |
| New_list=[] | |
| while i < len(list): | |
| if list[i] == "z": | |
| break | |
| New_list.append(list[i]) | |
| i=i+1 | |
| return New_list | |
| New_string_list=["a","j","k","p","k"] | |
| list=New_string_list | |
| print (stop_at_z(list)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## Below is a for loop that works. Underneath the for loop, rewrite the problem so that it does the same thing, but using a while loop instead of a for loop. Assign the accumulated total in the while loop code to the variable sum2. Once complete, sum2 should equal sum1. | |
| sum1 = 0 | |
| lst = [65, 78, 21, 33] | |
| for x in lst: | |
| sum1 = sum1 + x | |
| i=0 | |
| sum2=0 | |
| while i <len(lst): | |
| sum2=sum2+lst[i] | |
| i=i+1 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## Challenge: Write a function called beginning that takes a list as input and contains a while loop that only stops once the element of the list is the string ‘bye’. What is returned is a list that contains up to the first 10 strings, regardless of where the loop stops. (i.e., if it stops on the 32nd element, the first 10 are returned. If “bye” is the 5th element, the first 4 are returned.) If you want to make this even more of a challenge, do this without slicing. | |
| def beginning(list): | |
| i=0 | |
| New_list=[] | |
| while i < len(list): | |
| if list[i] == "bye": | |
| break | |
| New_list.append(list[i]) | |
| i=i+1 | |
| part1=New_list[:10] | |
| return part1 | |
| New_string_list=["alu","jam","kola","potol","kamranga","bye","morich"] | |
| list=New_string_list | |
| print (beginning(list)) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| while True: | |
| print("this phrase will always print") | |
| break | |
| print("Does this phrase print?") | |
| print("We are done with the while loop.") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## We’ll need these variables: | |
| total - this will start at zero | |
| count - the number of items, which also starts at zero | |
| moreItems - a boolean that tells us whether more items are waiting; this starts as True | |
| def checkout(): | |
| total = 0 | |
| count = 0 | |
| moreItems = True | |
| while moreItems: | |
| price = float(input('Enter price of item (0 when done): ')) | |
| if price != 0: | |
| count = count + 1 | |
| total = total + price | |
| print('Subtotal: $', total) | |
| else: | |
| moreItems = False | |
| average = total / count | |
| print('Total items:', count) | |
| print('Total $', total) | |
| print('Average price per item: $', average) | |
| checkout() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| x = 0 | |
| while x < 10: | |
| print("we are incrementing x") | |
| if x % 2 == 0: | |
| x += 3 | |
| continue | |
| if x % 3 == 0: | |
| x += 5 | |
| x += 1 | |
| print("Done with our loop! X has the value: " + str(x)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## Currently the function is supposed to take 1 required parameter, and 2 optional parameters, however the code doesn’t work. Fix the code so that it passes the test. This should only require changing one line of code. | |
| def waste(mar,var = "Water",marble = "type"): | |
| final_string = var + " " + marble + " " + mar | |
| return final_string |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Define a function called multiply. It should have one required parameter, a string. It should also have one optional parameter, an integer, named mult_int, with a default value of 10. The function should return the string multiplied by the integer. (i.e.: Given inputs “Hello”, mult_int=3, the function should return “HelloHelloHello”) | |
| def multiply(x,mult_int=10): | |
| return x*mult_int |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # this works | |
| names = ["Jack","Jill","Mary"] | |
| for n in names: | |
| print("'{}!' she yelled. '{}! {}, {}!'".format(n,n,n,"say hello")) | |
| # but this also works! | |
| names = ["Jack","Jill","Mary"] | |
| for n in names: | |
| print("'{0}!' she yelled. '{0}! {0}, {1}!'".format(n,"say hello")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| names_scores = [("Jack",[67,89,91]),("Emily",[72,95,42]),("Taylor",[83,92,86])] | |
| for name, scores in names_scores: | |
| print("The scores {nm} got were: {s1},{s2},{s3}.".format(nm=name,s1=scores[0],s2=scores[1],s3=scores[2])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## One very common pattern is called a listener loop. Inside the while loop there is a function call to get user input. The loop repeats indefinitely, until a particular input is received. | |
| theSum = 0 | |
| x = -1 | |
| while (x != 0): | |
| x = int(input("next number to add up (enter 0 if no more numbers): ")) | |
| theSum = theSum + x | |
| print(theSum) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| initial = 7 | |
| def f(x, y =3, z=initial): | |
| print("x, y, z, are: " + str(x) + ", " + str(y) + ", " + str(z)) | |
| f(2) | |
| f(2, 5) | |
| f(2, 5, 8) | |
| Output: | |
| x, y, z, are: 2, 3, 7 | |
| x, y, z, are: 2, 5, 7 | |
| x, y, z, are: 2, 5, 8 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def f(a, L=[]): | |
| L.append(a) | |
| return L | |
| print(f(1)) | |
| print(f(2)) | |
| print(f(3)) | |
| print(f(4, ["Hello"])) | |
| print(f(5, ["Hello"])) | |
| Output: | |
| [1] | |
| [1, 2] | |
| [1, 2, 3] | |
| ['Hello', 4] | |
| ['Hello', 5] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Write a function called str_mult that takes in a required string parameter and an optional integer parameter. The default value for the integer parameter should be 3. The function should return the string multiplied by the integer parameter. | |
| def str_mult(a,b=3): | |
| return a*b |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import random | |
| import turtle | |
| def isInScreen(w,t): | |
| leftBound = - w.window_width() / 2 | |
| rightBound = w.window_width() / 2 | |
| topBound = w.window_height() / 2 | |
| bottomBound = -w.window_height() / 2 | |
| turtleX = t.xcor() | |
| turtleY = t.ycor() | |
| stillIn = True | |
| if turtleX > rightBound or turtleX < leftBound: | |
| stillIn = False | |
| if turtleY > topBound or turtleY < bottomBound: | |
| stillIn = False | |
| return stillIn | |
| t = turtle.Turtle() | |
| wn = turtle.Screen() | |
| t.shape('turtle') | |
| while isInScreen(wn,t): | |
| coin = random.randrange(0, 2) | |
| if coin == 0: | |
| t.left(90) | |
| else: | |
| t.right(90) | |
| t.forward(50) | |
| wn.exitonclick() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## Let’s say you want a function that asks a yes-or-no question. In this case, you want to make sure that the person using your program enters either a Y for yes or N for no (in either upper or lower case). Here is a program that uses a while loop to keep asking until it receives a valid answer. | |
| def get_yes_or_no(message): | |
| valid_input = False | |
| while not valid_input: | |
| answer = input(message) | |
| answer = answer.upper() # convert to upper case | |
| if answer == 'Y' or answer == 'N': | |
| valid_input = True | |
| else: | |
| print('Please enter Y for yes or N for no.') | |
| return answer | |
| response = get_yes_or_no('Do you like lima beans? Y)es or N)o: ') | |
| if response == 'Y': | |
| print('Great! They are very healthy.') | |
| else: | |
| print('Too bad. If cooked right, they are quite tasty.') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## Write a while loop that is initialized at 0 and stops at 15. If the counter is an even number, append the counter to a list called eve_nums. | |
| eve_nums=[] | |
| count=0 | |
| while count<=15: | |
| if count%2==0: | |
| eve_nums.append(count) | |
| count=count+1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Below, we’ve provided a for loop that sums all the elements of list1. Write code that accomplishes the same task, but instead uses a while loop. Assign the accumulator variable to the name accum. | |
| list1 = [8, 3, 4, 5, 6, 7, 9] | |
| accum=0 | |
| idx=0 | |
| while idx < len(list1): | |
| accum = accum + list1[idx] | |
| idx=idx+1 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## Write a function called stop_at_four that iterates through a list of numbers. Using a while loop, append each number to a new list until the number 4 appears. The function should return the new list. | |
| def stop_at_four(list): | |
| New_list=[] | |
| i=0 | |
| while i < len(list): | |
| if list[i]==4: | |
| break | |
| New_list.append(list[i]) | |
| i=i+1 | |
| return New_list | |
| list=[1,2,3,4] | |
| stop_at_four(list) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def sumTo(aBound): | |
| """ Return the sum of 1+2+3 ... n """ | |
| theSum = 0 | |
| aNumber = 1 | |
| while aNumber <= aBound: | |
| theSum = theSum + aNumber | |
| aNumber = aNumber + 1 | |
| return theSum | |
| print(sumTo(4)) | |
| print(sumTo(1000)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Assessment 7 problem 5
"""
Write a function called checkingIfIn that takes three parameters.
The first is a required parameter, which should be a string.
The second is an optional parameter called direction with a default value of True.
The third is an optional parameter called d that has a default value of {'apple': 2, 'pear': 1, 'fruit': 19, 'orange': 5, 'banana': 3, 'grapes': 2, 'watermelon': 7}.
Write the function checkingIfIn so that when the second parameter is True, it checks to see if the first parameter is a key in the third parameter; if it is, return True, otherwise return False.
But if the second paramter is False, then the function should check to see if the first parameter is not a key of the third. If it’s not, the function should return True in this case, and if it is, it should return False.
"""