0

Can someone point me in the right direction to write this code please

Hi guys, I am having trouble writing this code I have like all the basic input prompts to get the input data from the user but how can I add all the input up to a total and then output messages? Please enter the route: Please enter the number of stops on this route: How many passengers were waiting for the bus at stop #1? 5 How many passengers left the bus at stop #2? 3 How many passengers were waiting for the bus at stop #2? 4 How many passengers left the bus at stop #3? 2 This is the last stop. There should be 4 passengers leaving. How many passengers left the bus at stop #3? 4 Route number: 999 Passengers left: 4

30th Apr 2017, 3:34 AM
Tom
4 Antworten
+ 6
The type of value returned by input() function is string. To do calculation with them, you need to cast them to numbers: def number(s): try: if '.' in s: return float(s) else: return int(s) except: return None ... use the above function to do that: value = number(input('Enter a value? ')) ... to get the most adapted numerical type ;) However, what do you want to do with your last lines of code? ( from declaration/assignation of 'a' and 'b' arrays ) What should be the purpose of your ( unused ) sum() function? ( actually she return the concatenation of two inaccessible global variables: you need to defines parameters to your function to pass any list through them, or define your "static" lists so they are accessible inside the function scope -- declare them inside, or declare 'a' and 'b' with 'global' keyword )
30th Apr 2017, 6:16 AM
visph
visph - avatar
+ 4
This is your output... what's your code? or is it your "expected" output? What is the purpose of the code to write? ( what to do with all these user entries ^^ ) @@
30th Apr 2017, 5:09 AM
visph
visph - avatar
0
Thnx @visph, no this is not my output, I have most of the user input prompts done I just need to add them up at the end here is what i got so far bus_route= 123 bus_route = input("Please enter the route number: ") if input() != bus_route: print("Invalid route number. Please enter a positive integer.") else: stops = input("Please enter the number of stops on this route: ") stops = int(stops) first = input("How many passengers were waiting for the bus at stop #1? ") first = int(first) first_stop = input("How many passengers left the bus at stop #2? ") first_stop = int(first_stop) second = input("How many passengers were waiting for the bus at stop #2? ") second = int(second) second_stop = input("How many passengers left the bus at stop #3? ") second_stop= int(second_stop) third = input("How many passengers were waiting for the bus at stop #3? ") third = int(third) third_stop = input("How many passengers left the bus at stop #4? ") third_stop= int(third_stop) fourth = input("How many passengers were waiting for the bus at stop #4? ") fourth = int(fourth) fourth_stop = input("How many passengers left the bus at stop #5? ") fourth_stop= int(fourth_stop) fifth = input("This is the last stop. There should be + 'fourth_stop' passengers leaving at this stop") fifth = int(fifth) total_stop= first+second+third+fourth+fifth print(total_stop) a = [first_stop, second_stop, third_stop, fourth_stop] b = [first, second, third, fourth, fifth] def sum(): return (a+b)
30th Apr 2017, 5:50 AM
Tom
0
I give that a go. Thank you @visph
30th Apr 2017, 6:28 AM
Tom