alder = int(input("Din alder: "))
reiserute = int(input("Hvilken reiserute: "))
def bilett_pris(alder, reiserute):
if alder <= 2:
return 0
rutepristabell = {
"rute1": 150,
"rute2": 250,
"rute3": 50,
}
rutepris = rutepristabell[reiserute]
alder_rabatt = 0.65
if alder <= 65:
alder_rabatt = 0
if alder <= 16:
alder_rabatt = 0.5
return rutepris - rutepris * alder_rabatt
print(bilett_pris(alder, reiserute))
Asked
Active
Viewed 31 times
0
-
Welcome to Stack Overflow. Please check that your indentation is correct. As is, the final ```return rutepris - rutepris * alder_rabatt``` looks like it's associated to the ```if alder <= 16:``` block – ewokx Oct 07 '22 at 07:04
-
@ewokx That wasn't the only indentation issue. – AKX Oct 07 '22 at 07:13
-
@AKX right. That last return line jumped out at me first more than others. But yeah, the indentation's confusing. – ewokx Oct 07 '22 at 07:36
1 Answers
0
A function that does not reach any other return
statement implicitly returns None
.
Python is an indentation sensitive language. There are two indentation problems in your code.
Firstly, looking at the start of your function
if alder <= 2:
return 0
rutepristabell = {
...
you can see that the entire function body is inside the alder <= 2
block. Since the first statement of that block is a return
, the rest of the function is unreachable.
Secondly, near the end,
if alder <= 16:
alder_rabatt = 0.5
return rutepris - rutepris * alder_rabatt
the other return
is also misindented to be inside the other if
, so even if you did fix the first issue, you'd only ever reach a return
if alder <= 16
.
The correct indentation is
def bilett_pris(alder, reiserute):
if alder <= 2:
return 0
rutepristabell = {
"rute1": 150,
"rute2": 250,
"rute3": 50,
}
rutepris = rutepristabell[reiserute]
alder_rabatt = 0.65
if alder <= 65:
alder_rabatt = 0
if alder <= 16:
alder_rabatt = 0.5
return rutepris - rutepris * alder_rabatt
and as you can see, there's always a definite return
now.

AKX
- 152,115
- 15
- 115
- 172
-
-
Traceback (most recent call last): File "/tmp/sessions/bd751c33b6710f4f/main.py", line 25, in
print (bilett_pris (alder, reiserute)) File "/tmp/sessions/bd751c33b6710f4f/main.py", line 15, in bilett_pris rutepris = rutepristabell[reiserute] KeyError: 3 – Nee Oct 07 '22 at 07:16 -
That's a different error. Your `rutepristabell` does not have a `3` key. Change `"rute3"` to `3`. – AKX Oct 07 '22 at 07:18