I have a problem with if, else python

2
# Viajes
TI=raw_input("Tipo de autobus: ");
KM=raw_input("Kilometros a recorrer: ");
NPR=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

By doing this exercise when I input if TI=A , if TI==A , Syntax gives me error. The only way I could put it was if TI:"A" thing I do not really understand.

Then when entering else it gives me another syntax error in the same way

I do not really know what I'm doing wrong

    
asked by Jose Martinez 19.05.2017 в 04:12
source

2 answers

2

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.

answered by 19.05.2017 в 04:45
1

You have several indentation errors, in some if and else you are missing the: and to receive numbers from an input, you must use only input, and not raw_input.

Here is your code working, at least with what you have so far:

# Viajes
TI=raw_input("Tipo de autobus: ");
KM=input("Kilometros a recorrer: ");
NPR=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

I hope I have helped you and have a good trip in the world of programming.

    
answered by 19.05.2017 в 04:45