0

I have 2 functions. If I call my first function from outside of the functions, it works. It gives the right value. Now if I call my first function inside my second function, it returns me with a value of 0.

I don't understand what I'm doing wrong. Could someone please point me into the right direction here? Much appreciated!

def standaardprijs(afstandKM):
 totaalPrijs = 0
 if afstandKM < 50:
 kmPrijs = 0.8
 totaalPrijs += kmPrijs * afstandKM
 return(totaalPrijs)
 if afstandKM > 50:
 totaalPrijs = totaalPrijs + 15
 kmPrijs = 0.6
 totaalPrijs += kmPrijs * afstandKM
 return totaalPrijs
def ritprijs(leeftijd, weekendrit, afstandKM):
 totaalPrijs = 0
 totaaalPrijs = standaardprijs(afstandKMInp)
 print(totaalPrijs)
 # Leeftijd
 if leeftijd >= 12 and leeftijd <= 65:
 if weekendRit == 'j':
 TP2 = totaalPrijs / 0.35
 return TP2
 else:
 TP2 = totaalPrijs / 0.3
 return TP2
 else:
 if weekendRit == 'j':
 TP2 = totaalPrijs / 0.4
 return TP2
 else:
 #geen korting
 TP2 = totaalPrijs
 return TP2
afstandKMInp = eval(input('Hoeveel KM gaat u reizen?'))
weekendRit = input('Weekendrit? j/n')
leeftijd = int(input('Leeftijd: '))
ritprijs2 = ritprijs(leeftijd, weekendRit, afstandKMInp)
print(ritprijs2)
Padraic Cunningham
181k30 gold badges264 silver badges327 bronze badges
asked Sep 8, 2016 at 10:12
2
  • Your code snippet is fairly complicated (and being not written in English is very hard to comprehend). Can you please create minimal reproducible example? Commented Sep 8, 2016 at 10:18
  • @Sone Name, I rolled back the edits, changing your question code makes answers obsolete. Commented Sep 8, 2016 at 11:01

2 Answers 2

1

ritprijs(leeftijd, weekendRit, afstandKMInp)

This line has no side effects. It returns a value but you never assign it to anything.

You should assign its return value to a variable:

some_value = ritprijs(leeftijd, weekendRit, afstandKMInp)
print(some_value)
answered Sep 8, 2016 at 10:18
Sign up to request clarification or add additional context in comments.

3 Comments

Edited my code. Now it still gives me 0 as the output. + Since I have a return on every possible outcome, I shouldn't have to print from outside my functions?
@SomeName, if you are running the code outside a shell you will need to print to see the return values
Running in PyCharm. But I'm getting 2 results now, both 0. So my return is working
0

The problem is you have a typo totaaalPrijs is not totaalPrijs so all you need to do is to change to:

 totaalPrijs = standaardprijs(afstandKMInp)

Also you should not use eval, cast input to int:

 afstandKMInp = int(input('Hoeveel KM gaat u reizen?'))

You can also simplify if leeftijd >= 12 and leeftijd <= 65 using a chained comparison:

 if 12 <= leeftijd <= 65:

And you don't need to declare variables in python so you can remove totaalPrijs = 0

answered Sep 8, 2016 at 10:44

4 Comments

Thanks, this worked! Now I got a new problem tho, it's not registering leeftijd somehow. In my function the variable leeftijd is black, weekendrit and afstandKM are both grey. No idea what it means. But it's not picking up on my leeftijd if's. Any idea? EDIT: PEP8 error. Shadows leeftijd from outer scoop.
@SomeName, because again weekendRit != weekendrit, a good reason why using lowercase and underscores for your variable names is a good idea, your code works as the function is using the weekendRit you declared outside the function in the global namespace
Wow, okay good call. Sorry just started python. Edited in main post. But that does not explain why leeftijd is not working? If I enter j together with leeftijd 16 it should return/print TP2 but I'm getting nothing
@SomeName, don't keep editing the question, your initial question has been resolved, instead of editing the question you should ask a new one but I would recommend you look at your code and make sure all the names align. Use lowercase and underscores for variable names as per the pep8 style guide python.org/dev/peps/pep-0008.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.