Piras314
Piras314

Reputation: 308

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

I'm running python 3.9.7 and am making a youtube video info viewer / downloader. Without the match statement the code runs fine and doesn't have any errors with concern to missing brackets. An interesting thing is that Atom doesn't show match with any colour in my code, however it seems not to do that in a blank file with only the match anyway.

# 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)

Edit: I'm running it in the terminal

Upvotes: 1

Views: 2416

Answers (2)

Piras314
Piras314

Reputation: 308

I had to install Python 3.10 from python.org because matching was only added in Python 3.10.

Upvotes: 3

user14461648
user14461648

Reputation:

When getting invalid syntax error on match statement, check that you have at least python 3.10 installed as this is the version it was released.

You can check your current version

cmd

python -V

python

import sys
print(sys.version)

Install the latest version of python (Windows)
Download installer: https://www.python.org/downloads/

or

Chocolatey update
cmd install chocolatey if not already (paste to admin 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"

Update python version (3.x)

choco upgrade python3 --pre

Sources:
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

Upvotes: 1

Related Questions