0
Hi everyone why does it give error?
https://sololearn.com/compiler-playground/c436DxiiU78C/?ref=app Note when I tried elif it worked well.
16 Réponses
+ 4
import sys
print(sys.version)
# What do you observe?
# What does it tell you about your present issue?
+ 3
match...case it is used in the newer version of python 3.10.
+ 3
Yusof ,
Sololearn apparently upgraded from 3.9.16 to 3.9.18 five days ago, but it's still too low to use the match compound statement.
If you're on Android, as I am, you can download Pydroid 3 from the Google Play Store.
It's a Python 3 IDE for Android. It's free with ads, or trial subscription becomes subscription (don't start that), or pay once for life (better deal). The ads don't play in airplane mode.
Pydroid 3 is currently running Python 3.11.4. I've experimented with match there.
+ 1
"Match" is similar (not equal) not the "switch" in other language.
https://docs.python.org/3/tutorial/controlflow.html#tut-match
In the given code, "n" is a string, while "n.endswith(...)" is a boolean. The case values would need to be of the same type as the match value.
+ 1
Yusof ,
Here's a refactoring that does the same job, but I won't be satisfied until I also understand why,
match True:
doesn't fix the TypeError in your first version.
https://sololearn.com/compiler-playground/cffGkCrIwZvU/?ref=app
+ 1
Wong Hei Ming ,
Yeah, but I'm talking about the substitution to his original code that Solo mentioned and I had already tried without success.
Original code:
n=input("enter file : ").lower()
match n:
case n.endswith(".gif"):
print("image/gif")
case n.endswith(".jpg"):
print("jpeg/image")
case n.endswith(".jpeg"):
print("jpeg/image")
case n.endswith(".ping"):
print("jpeg/image")
case n.endwith(".pdf"):
print("pdf/text")
case n.endswith(".txt"):
print("txt/text")
case n.endswith(".zip"):
print("zip/text")
case _:
print("applicaton/octet-stream")
That gets a TypeError, which one would think is because it tries to match str to bool.
So you change it from,
match n:
to,
match True:
in order to match bool to bool. Like so.
n=input("enter file : ").lower()
match True:
case n.endswith(".gif"):
print("image/gif")
case n.endswith(".jpg"):
print("jpeg/image")
[etc.]
but it still gets TypeError.
What causes that TypeError?
0
I tried it on online visual studio code it gives me type errorSolo
0
Yusof, try match True: ...😎
0
Solo ,
Did you get match True: to work? I thought of it too, but it still produced the same error for me.
TypeError: called match pattern must be a type
0
Rain, no, this is just my guess, (I had neither the opportunity nor the time to check it).
Based on logic "match n case n.endswith(".gif"):" – this is similar to the "if 'file.gif' == True:", which in principle is not true to compare, whereas "if True == True:" could fix the situation, since the method .endswith() returns a Boolean value.
Personally, I would use a dictionary here instead of a match...case, it's much simpler and more ergonomic in my opinion.
0
Solo ,
Yeah that's what I was trying too: since all the cases evaluate to bool, why not make the match bool? But the problem is apparently deeper and weirder.
0
Just tried with IDLE.
In the below code the first case runs, not the second.
n=input("enter file : ").lower()
match n:
case ".gif":
print("image/gif")
case n.endswith(".jpg"):
print("jpeg/image")
# It runs if the input of n is ".gif", but not "somevalue.jpg"
0
Rain
It should be match n: not match True:
n = True
match n:
case True:
print(True)
case False:
print(False)
case _:
print("Something else")
It prints True
0
You could
0
Add more like false and true code
0
Rain
I take a little time to look for the documentation and found this.
https://docs.python.org/3/reference/compound_stmts.html#match
Under 8.6.4.10, it says:
If name_or_attr is not an instance of the builtin type , raise TypeError.
My guess (probably wrong), match True: where the "True" in the statement is not an instance.