def cSelection():
Selection = input()
return Selection
if Selection == 1 :
print('Oxygen levels are normal')
elif Selection == 2:
print('Fuel levels are at medium capacity')
elif Selection == 3:
print('Food is running low, request for new shipment')
Asked
Active
Viewed 123 times
-1
1 Answers
0
The return
statement is not in right place. Your if/elif
conditions don't running due to the return
returns from the function before them. The return
should be after the logic (if/elif
).
Furthermore the input()
function returns a string type but your if/elif
contidions wait an integer. It means your should cast the output of input()
to integer with int()
.
I recommend to define an else
branch if the input is not 1-3. Like in my below example.
Correct code:
def cSelection():
Selection = int(input("Write a number (1-3): ")) # Input cast to integer.
if Selection == 1 :
print('Oxygen levels are normal')
elif Selection == 2:
print('Fuel levels are at medium capacity')
elif Selection == 3:
print('Food is running low, request for new shipment')
else:
print("Wrong option")
return Selection
return_value = cSelection()
print("Return value of function: {}".format(return_value))
Output:
>>> python3 test.py
Write a number (1-3): 1
Oxygen levels are normal
Return value of function: 1
>>> python3 test.py
Write a number (1-3): 3
Food is running low, request for new shipment
Return value of function: 3
>>> python3 test.py
Write a number (1-3): 5
Wrong option
Return value of function: 5

milanbalazs
- 4,811
- 4
- 23
- 45