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)
-
Your code snippet is fairly complicated (and being not written in English is very hard to comprehend). Can you please create minimal reproducible example?Łukasz Rogalski– Łukasz Rogalski2016年09月08日 10:18:30 +00:00Commented Sep 8, 2016 at 10:18
-
@Sone Name, I rolled back the edits, changing your question code makes answers obsolete.Padraic Cunningham– Padraic Cunningham2016年09月08日 11:01:44 +00:00Commented Sep 8, 2016 at 11:01
2 Answers 2
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)
3 Comments
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
4 Comments
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