Skip to main content

Seeking a Notebooks Command that requires a string value

Comments

2 comments

  • Debbie Alexander
    Moderator Beacon of Knowledge Super Star

    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
  • Micah M
    Beacon of Knowledge

    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.