匹配大小写无效语法,但没有匹配代码没有语法错误

Match case invalid syntax, but no syntax error without match code

我是 运行 python 3.9.7,正在制作一个 youtube 视频信息查看器/下载器。如果没有 match 语句,代码运行良好并且没有任何关于缺少括号的错误。有趣的是 Atom 在我的代码中没有显示与任何颜色的匹配,但是它似乎不会在只有匹配的空白文件中这样做。

# A youtube info and downloader
import getpass

from pytube import YouTube
from pathlib import Path

username = getpass.getuser()
downloads_path = str(Path.home() / "Downloads")

# Create video object
link = input("Enter video link (Don't forget https://): ")
video_object = YouTube(link)

# Print info
print(f"Title:  {video_object.title}")
print(f"Length: {round(video_object.length / 60, 2)} minutes")
print(f"Views:  {round(video_object.views / 1000000, 2)} million")
print(f"Author: {video_object.author}")

# Download
print("Download: (b)est | (w)orst | (a)udio | (e)xit")
download_choice = input(f"{username} $ ")

match download_choice:
    case: "b":
        video_object.streams.get_highest_resolution().download(downloads_path)

    case: "w":
        video_object.streams.get_worst_resolution().download(downloads_path)

    case: "a":
        video_object.streams.get_audio_only().download(downloads_path)

编辑: 我运行它在终端

必须从 python.org 安装 python3.10,因为 catch 匹配仅在 python 3.10

中添加

当 match 语句出现无效语法错误时,检查您是否至少安装了 python 3.10,因为这是它发布的版本。

您可以查看当前版本

命令

python -V

python

import sys
print(sys.version)

安装最新版本python (Windows)
下载安装程序:https://www.python.org/downloads/

巧克力更新
cmd 安装 chocolatey(如果尚未安装)(粘贴到管理员 shell)

@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "[System.Net.ServicePointManager]::SecurityProtocol = 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"

更新 python 版本 (3.x)

choco upgrade python3 --pre

来源:
https://www.delftstack.com/howto/python/how-to-update-python/
https://www.python.org/downloads/
https://docs.chocolatey.org/en-us/choco/setup#install-with-cmd.exe