Search code examples
pythonpylance

Python: Expected expression error on end if


I'm quite new to the Python programming language and am just working on a basic project to sort of try to grasp the basics. This may have been asked before but I can't really get a clear answer (most of the answers I see don't include end if). I'm not sure if it means that I've used wrong spacing somewhere or if I'm missing a semi colon (does Python have those?) somewhere.

My code

print("----Places to visit app---")
print("1. Insert a place")
print("2. Print places")
print("3. Delete a place")
print("4. Exit")
print("5. Please enter your choice")
option = input()
if option == '1':
    print("Please enter the name of the place: ")
    place_name = input()
    print("please enter the address of the place: ")
    place_address = input()
    print("please enter the number of days you plan to stay at this place: ")
    num_days = int(input())
    print("Please enter the amount you plan to spend per day")
    daily_cost=float(input())
    total_cost=num_days*daily_cost
    print("-----Place Added-----")
    print("Place name: ", place_name)
    print("Address: ", place_address)
    print("Number of Days: ", num_days)
    print("Total cost: $", total_cost)
elif option == '2':
   print("Print places feature will be develped in future...")
elif option == '3':
   print("Delete a place feature will be develped in future...")
elif option == '4':
   print("Goodbye!!!")
else:
   print("Invalid option selected. Please choose a valid option.")
end if

Error on line 31 (end if): Expected expression Pylance


Solution

  • I think you're mistaking some keywords.

    1. end if is not valid syntax, keyword in python
    2. Python is scoped by indentation. So your if block actually ended at the last print('Invalid...') line

    It's like this

    if true:
        # inside if
    else: # since you did not indent/tab here ends the if scope
        # inside else
        # this is also inside since indented
    # This is no longer inside, the block ended when there's nothing at the same indent level