Seeking a Notebooks Command that requires a string value
I know there are Python Notebook commands that require specific strings, e.g. "FORWARD" and those which allow strings but also allow other types of data, such as the say command, which will display integers, coordinates, etc.
I am seeking a Notebooks command which will allow a string and only a string, and therefor will fail in a situation like this. (Please note that this does NOT fail. Show Title, like Say, allows integers.) Micah M
error_check = int("5")
try:
show_title(error_check)
except:
show_title("Your variable is not of type String!","So sad!")
-
I can't find a way to add a new comment after the code box above, so here is what I have come to. Since I don't yet have a typical Minecraft command to test a variable to see if it is a proper string, I can check its type with this function, which returns a boolean response.
error_check = int("5")
say(isinstance(error_check,float))say(isinstance(error_check,int))say(isinstance(error_check,str))0 -
Python variables are dynamically typed, meaning you don't specify the type of the variables when defining them. This is different from statically typed languages (like Java) where the compiler complains if a variable is of the wrong type. This means you have to write code to check variable type if it's something you need to check. Python does have a built in function to do this though - type(). Without know exactly what you want your function to do, here is an example of one way that could look:
def myFunction(var):
if type(var)==str:
do code
else:
say("use a string instead")0
Please sign in to leave a comment.
Comments
2 comments