You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python代码中end if行出现Expected expression错误的解决咨询

问题解决:Pylance提示「预期表达式」错误(end if行)

错误原因

Python是缩进敏感语言,完全依靠缩进划分代码块,根本不需要end if这类语句来结束条件判断。你写的end if是VBA、Ruby等其他语言的语法,Python无法识别,因此Pylance报「Expected expression(预期表达式)」错误。

另外,Python不需要用分号结尾来结束语句,除非你要在同一行写多条语句(不推荐),所以你的错误和分号无关。

修正后的代码

直接删除最后一行的end if即可,完整修正代码如下:

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.")

补充说明

  • Python的if/elif/else条件判断,完全通过缩进区分代码块层级,缩进一致的代码属于同一个逻辑块。
  • 不要混淆Python与其他语言的语法规则,end if、endif这类结束标记在Python中都是多余的无效代码。

内容的提问来源于stack exchange,提问作者CloakedArrow

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 16:24:54