Python Classroom Series – 29/Jul/2020

Number Guessing Logic

  • Refer Here for number guessing question
  • Lets use our old friend Jarvis & Speak english Preview
  • Lets write plain english to communicate with jarvis
Hi Jarvis,
Generate a random number between 1 and 100 & remember it as target
continue till user wins or accepts defeat
Now ask user for entering his guess
if the number matches display success message
if the number doesnt match 
   find if the user has guessed the number higher than target 
       if yes ask him to guess a lower number
       else ask him to guess higher
       ask him to accept defeat and quit
  • If we want to speak with python3 we need to understand statements

Python Statements

  • In simple terms statements are the things you write to tell python what your program has do.
  • Python Coneptual Hierarchy
    1. Programs are composed of modules
    2. Modules are composed of statements
    3. Statements are composed of expressions
    4. Expressions create & process objects
  • Minimal Python Statement Set Preview
  • Till we discuss about modules in depth, each python file (filename.py) is a module
  • Lets create a python file which prints 10 numbers with following
print(1)
print(2)
print(3)
print(4)
print(5)
print(6)
print(7)
print(8)
print(9)
print(10)
  • In the above module each line with print represents a statement.
  • Now lets run the program Preview
  • Any line which starts with # will not be executed by python
print(1)
print(2)
print(3)
print(4)
print(5)
print(6)
print(7)
print(8) #print 8
#print(9)
print(10)

Preview

  • Now can we write a python program to print 1 million numbers
  • Setup this program in Visual Studio Code For installing Visual Studio Refer Here
  • Install python extension Preview
  • Understanding Intellisense Preview Preview
  • Execute the Program from vscode Preview
  • See the output of the program in the Terminal Preview

Getting into Debugging

  • Python executes the statement one after other and shows the output.
  • Shall we understand program execution? for that we need to debug
  • We need to tell editor (IDE) from where do you want to take control of execution and in that line enable breakpoint
  • Put the cursor the line where you want to take control from and Toggle Breakpoint Preview
  • Now start debugging Preview Preview
  • Now the program execution stops on line 1 and from there step over (Execute this line and stop and next line) Preview Preview

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin