Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem returning values
#3
(Jan-19-2019, 02:39 PM)stullis Wrote: Several things to fix here.

  1. 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.
  2. 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.
  3. 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.
  4. 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
Reply


Messages In This Thread
problem returning values - by Naito - Jan-19-2019, 12:58 PM
RE: problem returning values - by stullis - Jan-19-2019, 02:39 PM
RE: problem returning values - by Naito - Jan-19-2019, 04:37 PM
RE: problem returning values - by woooee - Jan-19-2019, 04:53 PM
RE: problem returning values - by Naito - Jan-19-2019, 05:02 PM
RE: problem returning values - by perfringo - Jan-19-2019, 05:16 PM
RE: problem returning values - by ichabod801 - Jan-19-2019, 06:19 PM
RE: problem returning values - by Naito - Jan-19-2019, 08:21 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Code is returning the incorrect values. syntax error 007sonic 6 1,241 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  Returning values from Gaussian fitting Laplace12 0 1,582 Aug-05-2021, 08:09 AM
Last Post: Laplace12
  returning values in for loop Nickd12 4 12,287 Dec-17-2020, 03:51 AM
Last Post: snippsat
  Problem adding keys/values to dictionary where keynames = "property" and "value" jasonashaw 1 2,059 Dec-17-2019, 08:00 PM
Last Post: jasonashaw
  Problem with sum of values from .txt file PathhK 2 2,588 Jan-07-2019, 07:40 PM
Last Post: nilamo
  Problem witrh else and elif values. anolibal 7 6,520 Aug-20-2018, 11:50 PM
Last Post: Skaperen
  I think this is a problem with returning? maby? TheNumericDolfin 8 4,129 Aug-17-2018, 08:12 PM
Last Post: TheNumericDolfin
  Class method returning multiple values,dont know which is returned when.. ujjwalrathod007 4 12,875 Oct-03-2016, 08:29 PM
Last Post: ujjwalrathod007

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020