You have two basic errors in your code:
-
raw_input
always returns a string so you must pass the string and integer type to be able to operate with the data.
-
You have syntax errors since after the conditionals ( if
, else
, elif
) you must use :
to separate the block of code to execute from the condition itself.
The code should be something like this:
TI = raw_input("Tipo de autobus: ")
KM = int(raw_input("Kilometros a recorrer: "))
NPR = int(raw_input("Numero de personas: "))
if TI == "A":
CK = 2000
else:
CK = 3000
if NPR<20:
NP = 20
else:
NP = NPR
TO = NP * CK * KM
CP = TO / NPR
print "La persona pagara: ", CP
print "El costo del viaje es: ", TO
As for the type of the bus. TI
is a string entered by the user. As a string it must be compared with another string, so you must use the quotes. If you do if TI:"A"
you are only checking that TI is not an empty string, not if it is "A", it must be if TI == "A"
. If A
and B
refer to variables that store strings then they will go without quotes, but still use the comparison operator ==
:
A = "A"
B = "B"
TI = raw_input("Tipo de autobus: ")
KM = int(raw_input("Kilometros a recorrer: "))
NPR = int(raw_input("Numero de personas: "))
if TI == A:
CK = 2000
else:
CK = 3000
if NPR<20:
NP = 20
else:
NP = NPR
TO = NP * CK * KM
CP = TO / NPR
print "La persona pagara: ", CP
print "El costo del viaje es: ", TO
Remember that in Python the indentation is crucial since, unlike other languages that mark with blocks or specific keywords the blocks of code, Python does it with the indentation. To not complicate your life try to follow these guidelines:
-
Always use spaces to devise, PEP 8 recommends using four spaces between each indentation level.
-
It is not advisable to use tabulations to devise, they are not very portable (a tabulation is not the same in all computers or editors) and they give headaches when they are hidden.
-
Never mix spaces and tabs . The editors and IDEs for Python usually come already configured to devise with four spaces (although use the Tab key to devise) but be careful if you use code copied from the web, there are still codes created using tabulations.