I try to fix this code many times but I don't know how to get the average of first quarter, second quarter, third quarter and fourth quarter of each students I hope I can get a help with this.
here is the code.
#set student grades
def studGrades(self,first,second,third,fourth):
self.stQ.append(first)
self.ndQ.append(second)
self.rdQ.append(third)
self.thQ.append(fourth)
print("Student Grade 1st Quarter: ", first)
print("Student Grade 2nd Quarter: ", second)
print("Student Grade 3rd Quarter: ", third)
print("Student Grade 4th Quarter: ", fourth)
#average of 1st,2nd,3rd,4th
def Average(self):
for i in self.stQ,self.ndQ,self.rdQ,self.thQ:
sums = self.stQ[i] + self.ndQ[i] + self.rdQ[i] + self.thQ[i]
avg = sums / 4
print(avg)
Number pattern:
input: 4
Output should :
1 2 3 4 17 18 19 20
5 6 7 14 15 16
8 9 12 13
10 11
Modify this function so that it can invert your dictionary. In particular, the function will need to turn each of the list items into separate keys in the inverted dictionary.
Run your modified invert_dict function on your dictionary. Print the original dictionary and the inverted one.
Include your Python program and the output in your Learning Journal submission.
Describe what is useful about your dictionary. Then describe whether the inverted dictionary is useful or meaningful, and why
Create a Python dictionary that returns a list of values for each key. The key can be whatever type you want.
Design the dictionary so that it could be useful for something meaningful to you. Create at least three different items in it. Invent the dictionary yourself. Do not copy the design or items from some other source.
Next consider the invert_dict function from Section 11.5 of your textbook.
# From Section 11.5 of:
# Downey, A. (2015). Think Python: How to think like a computer scientist. Needham, Massachusetts: Green Tree Press.
def invert_dict(d):
inverse = dict()
for key in d:
val = d[key]
if val not in inverse:
inverse[val] = [key]
else:
inverse[val].append(key)
return inverse
Write a loop over the strings in list test_miss and call missing_letters with each string. Print a line for each string listing the missing letters. For example, for the string "aaa", the output should be the following.
aaa is missing letters bcdefghijklmnopqrstuvwxyz
If the string has all the letters in alphabet, the output should say it uses all the letters. For example, the output for the string alphabet itself would be the following.
abcdefghijklmnopqrstuvwxyz uses all the letters
Print a line like one of the above for each of the strings in test_miss.
Submit your Python program. It should include the following.
The provided code for alphabet, test_dups, test_miss, and histogram.
Your implementation of the has_duplicates function.
A loop that outputs duplicate information for each string in test_dups.
Your implementation of the missing_letters function.
A loop that outputs missing letters for each string in test_miss.
Write a function called missing_letters that takes a string parameter and returns a new string with all the letters of the alphabet that are not in the argument string. The letters in the returned string should be in alphabetical order.
Your implementation should use a histogram from the histogram function. It should also use the global variable alphabet. It should use this global variable directly, not through an argument or a local copy. It should loop over the letters in alphabet to determine which are missing from the input parameter.
The function missing_letters should combine the list of missing letters into a string and return that string.
Write a function called has_duplicates that takes a string parameter and returns True if the string has any repeated characters. Otherwise, it should return False.
Implement has_duplicates by creating a histogram using the histogram function above. Do not use any of the implementations of has_duplicates that are given in your textbook. Instead, your implementation should use the counts in the histogram to decide if there are any duplicates.
Write a loop over the strings in the provided test_dups list. Print each string in the list and whether or not it has any duplicates based on the return value of has_duplicates for that string. For example, the output for "aaa" and "abc" would be the following.
aaa has duplicates
abc has no duplicates
Print a line like one of the above for each of the strings in test_dups.
Start with the following Python code.
alphabet = "abcdefghijklmnopqrstuvwxyz"
test_dups = ["zzz","dog","bookkeeper","subdermatoglyphic","subdermatoglyphics"]
test_miss = ["zzz","subdermatoglyphic","the quick brown fox jumps over the lazy dog"]
# From Section 11.2 of:
# Downey, A. (2015). Think Python: How to think like a computer scientist. Needham, Massachusetts: Green Tree Press.
def histogram(s):
d = dict()
for c in s:
if c not in d:
d[c] = 1
else:
d[c] += 1
return d
Copy the code above into your program but write all the other code
Describe how tuples can be useful with loops over lists and dictionaries, and give Python code examples. Create your own code examples.
Your descriptions and examples should include the following: the zip function, the enumerate function, and the items method.
Combined right angle triangle:
Sample input: 4
1st half:
1 2 3 4
5 6 7
8 9
10
2nd half:
17 18 19 20
14 15 16
12 13
11
Output should be combined pattern:
Combined pattern:
1 2 3 4 17 18 19 20
5 6 7 14 15 16
8 9 12 13
10 11