In [1]:
a = 0 # FIRST, set the initial value of the variable a to 0(zero).
while a < 10: # While the value of the variable a is less than 10 do the following:
a = a + 1 # Increase the value of the variable a by 1, as in: a = a + 1!
print(a) # Print to screen what the present value of the variable a is.
# REPEAT! until the value of the variable a is equal to 9!? See note.
# NOTE:
# The value of the variable a will increase by 1
# with each repeat, or loop of the 'while statement BLOCK'.
# e.g. a = 1 then a = 2 then a = 3 etc. until a = 9 then...
# the code will finish adding 1 to a (now a = 10), printing the
# result, and then exiting the 'while statement BLOCK'.
# --
# While a < 10: |
# a = a + 1 |<--[ The while statement BLOCK ]
# print (a) |
#
In [5]:
%%html
<iframe width="800" height="500" frameborder="0" src="http://pythontutor.com/iframe-embed.html#code=a+%3D+0++++++++++++%0D%0Awhile+a+%3C+10%3A++++%0D%0A++++a+%3D+a+%2B+1+++++%0D%0A++++print(a)+&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=3&rawInputLstJSON=%5B%5D&curInstr=1&codeDivWidth=350&codeDivHeight=400"> </iframe>
In [ ]:
In [6]:
a = 1
s = 0
print('Enter Numbers to add to the sum.')
print('Enter 0 to quit.')
while a != 0:
print('Current Sum:', s)
a = float(input('Number? '))
s = s + a
print('Total Sum =', s)
In [7]:
%%html
<iframe width="800" height="500" frameborder="0" src="http://pythontutor.com/iframe-embed.html#code=a+%3D+1%0D%0As+%3D+0%0D%0Aprint('Enter+Numbers+to+add+to+the+sum.')%0D%0Aprint('Enter+0+to+quit.')%0D%0Awhile+a+!%3D+0%3A+++++++++++++++++++++++++++%0D%0A++++print('Current+Sum%3A',+s)++++++++++++%0D%0A++++a+%3D+float(input('Number%3F+'))++++++++%0D%0A++++s+%3D+s+%2B+a++++++++++++++++++++++++++++%0D%0Aprint('Total+Sum+%3D',+s)&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=3&rawInputLstJSON=%5B%2212%22%5D&curInstr=7&codeDivWidth=350&codeDivHeight=400"> </iframe>
In [8]:
# This program calculates the Fibonacci sequence
a = 0
b = 1
count = 0
max_count = 20
while count < max_count:
count = count + 1
print(a, end=" ") # Notice the magic end=" " in the print function arguments
# that keeps it from creating a new line.
old_a = a # we need to keep track of a since we change it.
a = b
b = old_a + b
print() # gets a new (empty) line.
In [7]:
In [9]:
%%html
<iframe width="800" height="500" frameborder="0" src="http://pythontutor.com/iframe-embed.html#code=%23+This+program+calculates+the+Fibonacci+sequence%0D%0Aa+%3D+0%0D%0Ab+%3D+1%0D%0Acount+%3D+0%0D%0Amax_count+%3D+20%0D%0A+%0D%0Awhile+count+%3C+max_count%3A%0D%0A++++count+%3D+count+%2B+1%0D%0A++++print(a,+end%3D%22+%22)++%23+Notice+the+magic+end%3D%22+%22+in+the+print+function+arguments++%0D%0A+++++++++++++++++++++++%23+that+keeps+it+from+creating+a+new+line.%0D%0A++++old_a+%3D+a++++%23+we+need+to+keep+track+of+a+since+we+change+it.%0D%0A++++a+%3D+b%0D%0A++++b+%3D+old_a+%2B+b%0D%0Aprint()++%23+gets+a+new+(empty)+line.&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=3&rawInputLstJSON=%5B%5D&curInstr=2&codeDivWidth=350&codeDivHeight=400"> </iframe>
In [10]:
# Simplified and faster method to calculate the Fibonacci sequence
a = 0
b = 1
count = 0
max_count = 10
while count < max_count:
count = count + 1
print(a, b, end=" ") # Notice the magic end=" "
a = a + b
b = a + b
print() # gets a new (empty) line.
In [ ]:
In [11]:
%%html
<iframe width="800" height="500" frameborder="0" src="http://pythontutor.com/iframe-embed.html#code=%23+Simplified+and+faster+method+to+calculate+the+Fibonacci+sequence%0D%0Aa+%3D+0%0D%0Ab+%3D+1%0D%0Acount+%3D+0%0D%0Amax_count+%3D+10%0D%0A+%0D%0Awhile+count+%3C+max_count%3A%0D%0A++++count+%3D+count+%2B+1%0D%0A++++print(a,+b,+end%3D%22+%22)++%23+Notice+the+magic+end%3D%22+%22%0D%0A++++a+%3D+a+%2B+b++++%0D%0A++++b+%3D+a+%2B+b%0D%0Aprint()++%23+gets+a+new+(empty)+line.&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=3&rawInputLstJSON=%5B%5D&curInstr=0&codeDivWidth=350&codeDivHeight=400"> </iframe>
Enter Password
In [12]:
# Waits until a password has been entered. Use Control-C to break out without
# the password
#Note that this must not be the password so that the
# while loop runs at least once.
password = str()
# note that != means not equal
while password != "unicorn":
password = input("Password: ")
print("Welcome in")
In [13]:
%%html
<iframe width="800" height="500" frameborder="0" src="http://pythontutor.com/iframe-embed.html#code=%23+Waits+until+a+password+has+been+entered.++Use+Control-C+to+break+out+without%0D%0A%23+the+password%0D%0A+%0D%0A%23Note+that+this+must+not+be+the+password+so+that+the+%0D%0A%23+while+loop+runs+at+least+once.%0D%0Apassword+%3D+str()%0D%0A+%0D%0A%23+note+that+!%3D+means+not+equal%0D%0Awhile+password+!%3D+%22unicorn%22%3A%0D%0A++++password+%3D+input(%22Password%3A+%22)%0D%0Aprint(%22Welcome+in%22)&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=3&rawInputLstJSON=%5B%5D&curInstr=0&codeDivWidth=350&codeDivHeight=400"> </iframe>