(Jan-19-2019, 02:39 PM)stullis Wrote: Several things to fix here.
- The variable "dict" should not be named "dict". Dict is an object name in Python and should be considered reserved, just like a keyword. Call it "scores" instead.
- Your dict also has a problem. Because you're using the arguments as keys and the arguments are integers, there is the possibility of entering duplicate keys; for instance, math and science are both 20. In that case, one of your keys would disappear. The keys in your dict should be strings and the variables should be the values.
- I'm not sure what the loop is trying to accomplish but it isn't right. "i" would be an integer because that's what your keys were in the dict, then it's being checked as a boolean, and then incremented. That's inconsistent.
- len(dict.keys()) will always return 5 because there are 5 key/value pairs in the dict. To accomplish what you want, you should use a loop to iterate over the dict, evaluate the value of the current key, and increment a counter if the evaluation is true.
To further improve it once working, change the parameters to a single dict or **kwargs. For an advanced challenge, this function can be written on a single line...
thank you stullis,
well i didn't complete the courses of python im halfway through and i started coding for 1 week or so.
so about the loop i had no idea that loops works differently with dictionarys i thought that the loop will recognise the key and add the value but that's not the case xD, as i said i didn't complete the courses i make these codes to grasp the fundamentals but i end up discovering new details xD, so i did a small research and changes the code to this but it doesn't work as it should be
def IQcalculator(lang, math, sci, comp_sci, arts): scores = {'lang': [0], 'math': [0], 'sci': [0], 'comp_sci': [0], 'arts': [0] } if lang >= 10: scores[lang] = True else: scores[lang] = False if math >= 10: scores[math] = True else: scores[math] = False if sci >= 10: scores[sci] = True else: scores[sci] = False if comp_sci >= 10: scores[comp_sci] = True else: scores[comp_sci] = False if arts >= 10: scores[arts] = True else: scores[arts] = False for key in scores: if key is True: scores[key] += 1 else: scores[key] = 0 print("Your IQ IS: ", len(scores.values())) lang = int(input("your avr marks in languages: ")) math = int(input("your avr marks in maths: ")) sci = int(input("your avr marks in science: ")) comp_sci = int(input("your avr marks in computer science: ")) arts = int(input("your avr marks in arts: ")) IQcalculator(lang, math, sci, comp_sci, arts)
Output:your avr marks in languages: 14
your avr marks in maths: 14
your avr marks in science: 13
your avr marks in computer science: 14
your avr marks in arts: 14
Your IQ IS: 7