Creating an integer in robot mesh python

Could someone send an example of creating an interger? I know how to make a variable, I usually say (name of variable) = None
But when I make something equal to a number
Ex:
Distance = 1
All it does is make distance ==1 but it’s just a variable and not an integer. Please help on making something an integer. It’s easier with cpp because you can say-
int Distance = 1

2 Likes

I am pretty sure just setting something to a number allocates whatever memory it needs and classifies it as a (whatever number type). So your code technically does make an integer. An integer is a type of variable.
https://www.programiz.com/python-programming/numbers
https://www.tutorialspoint.com/python/python_numbers.htm
So, you could argue that it is easier with Python (I still prefer C++).

5 Likes

In python, variables become initialized whenever you use them in the code. (i.e. you don’t have to “create” or initialize the integer, it will create its self)

EDIT: Sniped by @trontech569, his explanation was better.

4 Likes

What’s the difference between making distance equal to one, and creating an integer called distance? What are you trying to do that’s not working with this/how is this different from the behaviour you were expecting?

6 Likes

What I’m trying to do is something like this
Distance = 1
While true:
If joystick.buttonA.pressing():
Distance = Distance + 1

This line should be that whenever I press A, the distance variable should go from 1-2 and every time I press it, the variable increases by 1, but this isnt working and instead it’s just saying that the variable distance is LITERALLY equal to Distance + 1

That snippet looks okay to me - could you post the exact code you’re having trouble with (remembering to wrap it in [code]...[/code] tags for formatting)?

Also, I’m not quite sure what you mean by

distance is LITERALLY equal to Distance + 1

could you expand on this?

4 Likes

You might need to use some kind of sleep function, so it only has enough time to add the number once. like this:

Distance = 1
while True:
     if joystick.buttonA.pressing():
         Distance = Distance + 1 #Distance += 1 does the same thing
         sys.sleep(0.2)
3 Likes

Variables are containers for storing data values. Unlike other programming languages, Python has no command for declaring a variable. A variable is created the moment you first assign a value to it.

2 Likes

Reminder, that if you use a variable, always declare it ahead of time and set it equal to SOME value. An accidental usage when it’s undeclared can cause undesired results.