Sublime Forum

New to Sublime and python

#1

I am trying to get simple math problems to run on my sublime text 3.

If I enter in 2 + 2 I get no output on at the bottom.

0 Likes

#2

Are you typing the data in the console?

>>> 2+2
4
>>> print( 'test' )
test
>>> type( () )
<class 'tuple'>
>>> type( () ) is tuple
True
>>> type( {} )
<class 'dict'>
>>> type( {} ) is dict
True
>>> type( [] )
<class 'list'>
>>> type( [] ) is list
True
>>> type( [] ) is str
False
>>> type( "Test" )
<class 'str'>
>>> type( "Test" ) is str
True

At the bottom left there’s a tiny icon - click it and click console to open up a white-background panel… There you can test things…

Also - it’s pretty basic… Trying to use python in cmd.exe / Command Prompt lets you create functions, etc… with this console you end up with errors…

C:\Users\Acecool>py
Python 3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> def IsSet( _data = None ): return _data != None
...
>>> IsSet( '' )
True
>>> IsSet(  )
False
>>> IsSet( None )
False
>>>

vs

>>>  def IsSet( _data = None ): return _data != None
  File "<string>", line 1
    def IsSet( _data = None ): return _data != None
    ^
IndentationError: unexpected indent

0 Likes

#3

Ok I checked it on the command prompt and it worked.

here is the code I wrote in question.

alien_0 = {“x_position”: 0, “y_position”: 25, “speed”: “slow”}
print("Original x-position " + str(alien_0[“x_position”]))

#move the alien to the right.
#Determine how far to move the alien based on its current speed.
if alien_0[“speed”] == “slow”:
x_increment = 1
elif alien_0[“speed”] == “medium”:
x_increment = 2
else:
# This must be the fast alien.
x_increment = 3

#The new position is the old position plus the increment. 

alien_0[“x-position”] = alien_0[‘x_position’] + x_increment
print("New x-position: " + str(alien_0[“x_position”]))

output on my sublime =
Original x-position 0
New x-position: 0

output should be =
Original x-position 0
New x-position: 2

Help!!!

0 Likes

#4

alien_0[“x-position”] isn’t >>> alien_0[“x_position”]

Simple case of a small typo making an issue…

Note: If you want to move to other languages - remember that Python is a high level language ( is does a lot for us )… In lower level languages, defining x_increment inside of an if statement will make it local to that scope and deeper meaning outside would see it as undefined… In Python this isn’t the case so your code works… But, just a heads up if you want to move around - keeping this in mind will help… Python introduces some bad habits, unfortunately, but it is an excellent language to work with and learn from.

I’d also recommend altering what you have into a basic class… For example:

##
## Simple Alien Class Example
##


##
## Basic ENUMeration - so when we type in code it is more human readable - but we'll be using these values as is without translating them...
## ie they become CONSTants - the difference is one the values mean nothing ( enumeration ) - as long as the values are unique, and with CONSTants,
## 	the values are just as important as the names - ie EARTH_GRAVITY = 9.81 - so if we use EARTH_GRAVITY in code, it is more readable than 9.81 plopped in..
##
ALIEN_MOVEMENT_STOPPED	= 0
ALIEN_MOVEMENT_SLOW		= 1
ALIEN_MOVEMENT_MEDIUM	= 2
ALIEN_MOVEMENT_FAST		= 3

# These will act as ENUMeration...
MOVE_DIR_RIGHT			= 0
MOVE_DIR_LEFT			= 1
MOVE_DIR_UP				= 2
MOVE_DIR_DOWN			= 3


##
## Alien Class
##
class Alien:
	##
	## This is called when Alien( ) is called - _name is set to Alien by default and _speed is Stopped by default... You can use Alien( 'Player X', ALIEN_MOVEMENT_FAST ) as an example... This creates a new object...
	##
	def __init__( self, _name = 'Alien', _speed = ALIEN_MOVEMENT_STOPPED ):
		# Set the default x and y position ( Later you can set this to a Pos object and build on it )
		self.x = 0
		self.y = 0

		# Update Speed
		self.SetSpeed( _speed )

		# Give it a unique name...
		self.SetName( _name )


	##
	## Set "this" aliens' speed - default is none so alien.SetSpeed( ) will reset the speed to 0...
	##
	def SetSpeed( self, _speed = ALIEN_MOVEMENT_STOPPED ):
		self.speed = _speed


	##
	## Helper - this returns the speed - useful if we want to apply extra logic to it...
	##
	def GetSpeed( self ):
		return self.speed


	##
	## Set "this" aliens' name
	##
	def SetName( self, _name = 'Alien' ):
		self.name= _name


	##
	## Helper - this returns the name
	##
	def GetName( self ):
		return self.name


	##
	## Move "this" alien in the direction we choose... default is right
	##
	def Move( self, _direction = MOVE_DIR_RIGHT ):
		# If the alien can't move, prevent the rest of the logic from running ( isn't exactly needed because the speed will be 0 ) and print a helpful notice...
		if ( self.GetSpeed( ) == ALIEN_MOVEMENT_STOPPED ):
			print( 'This alien, named ' + self.GetName( ) + ' is unable to move!' )
			return

		# Depending on the direction of the alien, move the alien...
		if ( _direction == MOVE_DIR_RIGHT ):
			self.x += self.GetSpeed( )
		elif ( _direction == MOVE_DIR_LEFT ):
			self.x -= self.GetSpeed( )
		elif ( _direction == MOVE_DIR_UP ):
			self.y -= self.GetSpeed( )
		elif ( _direction == MOVE_DIR_DOWN ):
			self.y += self.GetSpeed( )


	##
	## Helper - prints the current alien status! We need to convert the numbers to a string...
	##
	def ToString( self ):
		return 'Alien: ' + self.GetName( ) + ' is at position: ( x: ' + str( self.x ) + ', y: ' + str( self.y ) +' ) with a movement speed of: ' + str( self.GetSpeed( ) ) + '!'


#
# Usage
#

# Create them
_alien1 = Alien( 'Player', ALIEN_MOVEMENT_FAST )
_alien2 = Alien( 'Player 2', ALIEN_MOVEMENT_SLOW )
_alien3 = Alien( 'Player 3', ALIEN_MOVEMENT_MEDIUM )
_alien4 = Alien( 'Player 4', ALIEN_MOVEMENT_STOPPED )

# Move them 10 times..
for _i in range( 10 ):
    # This should end up at x: 30, y: 0
    _alien1.Move( MOVE_DIR_RIGHT )

    # This should end up at x: 0, y: 10
    _alien2.Move( MOVE_DIR_DOWN )

    # This should be at x: -20, y: 0
    _alien3.Move( MOVE_DIR_LEFT )

    # This one can't move
    _alien4.Move( MOVE_DIR_UP )

# Now print out their locations
print( 'All of the aliens started at x: 0, y: 0 and now they are here: ' )
print( _alien1.ToString( ) )
print( _alien2.ToString( ) )
print( _alien3.ToString( ) )
print( _alien4.ToString( ) )

I should’ve added getters / setters for x and y but for this basic example I simply did speed… I hope this helps…

Note: There may be a typo in mine - I’ll double check and edit…

Edit: Updated:

Output:

cmd.exe > py “Dropbox\Public\tutoring_python\classes_and_inheritance\simple_alien_class.py”
This alien, named Player 4 is unable to move!
This alien, named Player 4 is unable to move!
This alien, named Player 4 is unable to move!
This alien, named Player 4 is unable to move!
This alien, named Player 4 is unable to move!
This alien, named Player 4 is unable to move!
This alien, named Player 4 is unable to move!
This alien, named Player 4 is unable to move!
This alien, named Player 4 is unable to move!
This alien, named Player 4 is unable to move!
All of the aliens started at x: 0, y: 0 and now they are here:
Alien: Player is at position: ( x: 30, y: 0 ) with a movement speed of: 3!
Alien: Player 2 is at position: ( x: 0, y: 10 ) with a movement speed of: 1!
Alien: Player 3 is at position: ( x: -20, y: 0 ) with a movement speed of: 2!
Alien: Player 4 is at position: ( x: 0, y: 0 ) with a movement speed of: 0!



This one is very useful in Classes… It creates 3 functions by calling self.AccessorFunc( ‘Blah’, False ) – self.GetBlah( _default_override ), self.GetBlahToString( _default_override ) to force the data returned from GetBlah to be a string, and self.SetBlah( _value ) to set the value…

Basically, if this existed in the function above, then the definition lines for the Set / Get functions for Name and Speed could be replaced with 2 lines in init

Alien class as above but extended to use AccessorFunc to shorten a few things… adds Pos class to take over for x and y managing…

The lines would be shorter without the AccessorFunc function added to it ( should be in an import )…

0 Likes

#5

Acecool. Thank you for the help and the lesson!

Take care.

0 Likes