Example that triggers the error: "Invalid Syntax"

1

I am beginning to learn to program in Python through a book. They gave me this example but when I try to run the program I get "Invalid Syntax".

>>> userInput= input("Enter 1 or 2:")
Enter 1 or 2:
        if userInput == "1":
    print("Hello World")
    print("How are you")
    elif userInput == "2":
    print("Python Rocks")
    print("I love Python")
        else:
    print("You did not enter a valid number")
    
asked by Juan Carlos 28.01.2018 в 11:05
source

1 answer

1

The problem you have at the beginning the input in its syntax does not have ":" in the end, you can not place outside its parentheses a string like the one you have placed "Enter 1 or 2 ". This is why it says "invalid syntax" .

Also tell you that what input () does is to receive values entered by the user through the keyboard.

Here I have corrected your code:

print("Enter 1 or 2")
userInput= input() 
if userInput == "1": 
    print("Hello World") 
    print("How are you") 
elif userInput == "2": 
    print("Python Rocks") 
    print("I love Python") 
else: 
    print("You did not enter a valid number")

Note: In python it is very important to devise because if you do not do it well the console will also throw you error for it.

    
answered by 28.01.2018 в 11:57