Issue
How can i add the travel fare into a while loop (error message) that works with many travel fares? I want to create a function using the while loop. like without inserting the error message after each travelFare, i want to create a function to stop myself from repeating. and my using spyder.
travelfare1=float(input("enter travel fare 1:" ))
travelfare2=float(input("enter travel fare 2: "))
travelfare3=float(input("enter travel fare 3: "))
while travelfare1 <5 or travelfare1 >20:
print("invalid entry")
travelfare1=float(input("enter travel fare 1:" ))
please help
Solution
try this out:
def get_fare(mini, maxi):
fare = float(input("travel fare please: "))
while fare not in range(mini, maxi+1):
print("invalid value, please enter a new one")
fare = float(input("travel fare please: "))
return fare
travel_fares = [get_fare(5,20), get_fare(5,20), get_fare(5,20)] # list of the functions (which will make a list of the return values after they're run)
print(f"average: {sum(travel_fares)/len(travel_fares)}") # sum of elements in list divided by length of list (amount of items)
when entering: 15
, 10
and 5
:
travel fare please: 5
travel fare please: 10
travel fare please: 15
average: 10
Answered By - Ironkey
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.