Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem returning values
#1
Hi, i made iam a total beginner and i made this code for learning purposes, this code let you calculate your IQ out of 5 (n/5) by the average marks you get in 5 subjects (if mark is >=10, return +1),(sorry for the dumb code idea xD)
the problem is the output is always different than excpected (logical problem)

print("what's your IQ application 'n/5'")
print("tell us your average mark in every subject and we'll tell you your IQ ^^")

# try
def IQcalculator(lang, math, sci, comp_sci, arts):
    dict = {lang: 0,
            math: 0,
            sci: 0,
            comp_sci: 0,
            arts: 0}
    for i in dict:
        if i is True:
            i += 1
        else:
            i += 0

    if lang >= 10:
        dict[lang] = True
    else:
        dict[lang] = False
    if math >= 10:
        dict[math] = True
    else:
        dict[math] = False
    if sci >= 10:
        dict[sci] = True
    else:
        dict[sci] = False
    if comp_sci >= 10:
        dict[comp_sci] = True
    else:
        dict[comp_sci] = False
    if arts >= 10:
        dict[arts] = True
    else:
        dict[arts] = False
    print("Your IQ IS: ", len(dict.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)
random output:
Output:
your avr marks in languages: 6 your avr marks in maths: 7 your avr marks in science: 14 your avr marks in computer science: 3 your avr marks in arts: 20 Your IQ IS: 5
as you can see the supposed output is 2 but it's 5
thanks for help!
Reply
#2
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...
Reply
#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
#4
Print the dictionary named scores wherever you want, but definitely after the if/else blocks.
Reply
#5
(Jan-19-2019, 04:53 PM)woooee Wrote: Print the dictionary named scores wherever you want, but definitely after the if/else blocks.
the out put is (i entered same values as before):
Output:
{'lang': 0, 'math': 0, 'sci': 0, 'comp_sci': 0, 'arts': 0, 14: 0, 13: 0}
why the code added 14:0, 13:0 im so confused
Reply
#6
Can you tell in plain english what you want accomplish?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
(Jan-19-2019, 05:02 PM)Naito Wrote: why the code added 14:0, 13:0 im so confused

In your function, int, math, sci and all those variables are all integers. That is what you pass to the function on line 43. So then when you say scores[sci] = True, the key you are setting to True is an integer. In your last example you entered 13 for sci, so that line is equivalent to scores[13] = True.

Then you go through and replace all of the values. If key is True you add one to their value, otherwise you set them to zero. But key is never True. "key is True" doesn't mean that key resolves to True, or key is equivalent to True, it means key and True are stored in the exact same place in memory. Don't do conditions with if key is True: or even if key == True:. Just use if key:. If key resolves to True, then that will trigger.

But that's not what you want. You don't want to test the keys, you want to test the values. You are sticking the values into a dictionary so that you can test the keys. Why? get rid of the dictionary and just test the values.

def IQcalculator(lang, math, sci, comp_sci, arts):
    score = 0
    if lang > 10:
        score += 1
    if math > 10:
        score += 1
    if sci > 10:
        score += 1
    if comp_sci > 10:
        score += 1
    if arts > 10:
        score += 1
    print('Your IQ is {}.'.format(score))
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
Thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Returning values from Gaussian fitting Laplace12 0 135 Aug-05-2021, 08:09 AM
Last Post: Laplace12
  returning values in for loop Nickd12 4 774 Dec-17-2020, 03:51 AM
Last Post: snippsat
  Problem adding keys/values to dictionary where keynames = "property" and "value" jasonashaw 1 850 Dec-17-2019, 08:00 PM
Last Post: jasonashaw
  Problem with sum of values from .txt file PathhK 2 1,277 Jan-07-2019, 07:40 PM
Last Post: nilamo
  Problem witrh else and elif values. anolibal 7 4,118 Aug-20-2018, 11:50 PM
Last Post: Skaperen
  I think this is a problem with returning? maby? TheNumericDolfin 8 2,015 Aug-17-2018, 08:12 PM
Last Post: TheNumericDolfin
  Class method returning multiple values,dont know which is returned when.. ujjwalrathod007 4 4,046 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