Tuples, dictionary, and helper function problems in Python

Multi tool use
Multi tool use
The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Tuples, dictionary, and helper function problems in Python



So I had an assignment where I took a list of tuples as an argument such as,


[('A', 3), ('C', 4), ('B', 2), ('P', 4)]



and I had to compute the overall gpa which is basically((letter-grade-number * number of credits) / number of credits). So I had to use a dictionary and my body was composed of 3 parts. The first used a helper function that appended the unweighted grades to a list and used a counter to keep track of all the credits that were taken, the second part was a conditional that returned -1 if all the lists were not considered for grading and the last part finally summed all the values in the appended list and divided it by the total number of credits taken and it works perfectly.


((letter-grade-number * number of credits) / number of credits)


-1



Now, my next assignment takes a string as an argument such as:


'Jane A 3 A- 3 F 1#Bob C+ 2 D+ 3 B+ 3#Chris C 4 C 3 C- 2#Susie F 2 NC 1'



I figured I can simply turn this string into a list of tuples however, once I convert the first iteration of the string (which is marked by the # sign in the parameter), it is



['Jane', 'A', '3', 'A-', 3, 'F', 1]


['Jane', 'A', '3', 'A-', 3, 'F', 1]



I used a helper function to slice the string, turn it to a list, take only the letter grades and integers and zipped it into a tuple. So my helper function converts the first iteration of my initial string into a tuple that my previous code takes in its parameter:



[('A', 3), ('A-', 3), ('F', 1)]


[('A', 3), ('A-', 3), ('F', 1)]



When I input that tuple in my previous code, it gives me the right answer, however, when I try to call it after my first helper function returns the tuple, I get an iteration error.



My code is quite long, about 60 lines and I do not think I am allowed to post it here so if anyone is willing to take a look, here is my pastebin --- https://pastebin.com/3kG7WXAp





Please read about the Minimal, Complete, and Verifiable example and cut down to a specific issue.
– jonrsharpe
9 mins ago





Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described.
– Prune
7 mins ago





Including the stack trace of your error can also be helpful, in addition to cutting down to a MCV example as mentioned by others.
– CoffeeTableEspresso
5 mins ago





I didn't know I pasted my code on the post. I thought I put a link to my code using pastebin. I'm sorry.
– Seungsoo Im
4 mins ago





pastebin.com/3kG7WXAp
– Seungsoo Im
4 mins ago




1 Answer
1



Dunno if it fixes all you problems, but it fixed the errors you had:


weight = float(key1) * float(tup_val)



and


numbers_of_credit += float(i[1])






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

sXeqgxrPueFl PWTLe5TF6s
hm3 evKHQ7yw8334FtYseJXZCQbADV,xYkGKuEFcX7L9 Ow,lhLm2Oe5OZZioPzIH 3xJisx2mdA4mxi2,7RlCOGFO rZH9tVlh4

Popular posts from this blog

Makefile test if variable is not empty

Visual Studio Code: How to configure includePath for better IntelliSense results

Will Oldham