0% found this document useful (0 votes)
37 views3 pages

Untitled 8

Uploaded by

jalenkatzen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views3 pages

Untitled 8

Uploaded by

jalenkatzen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

num1 = input("input a number ")

num2 = input("input a second number ")


print "\n\n"
smaller = 0
larger = 0
total = 0

if num1 > num2:


larger = num1
smaller = num2
else :
larger = num2
smaller = num1

# I WROTE TWO WAYS FOR YOU TO DO IT BABY !!!!!

# While Loop Explanation: We set up a while loop. A while loop is an

# Easy way to to say "while this statement is true, then keep doing this thing"

# Below for example we are saying. As long as or "while" the smaller number is

# less than, or equal to the larger number (smaller <= larger), then the program
will take

# what the smaller number is at the moment and add it too the total (total = total
+ smaller).

# After this we will increment the smaller number by 1 (smaller = smaller +1) and
this

# prcess will only happen until the smaller number has been incremented enough
times until

# it is equal to the larger number

# To see what i mean run the program

print "WHILE LOOP BEGINNING: \n"


while smaller <= larger:
print("Variables before incrementing smaller number:\nSmaller: " +
str(smaller) + "\nLarger: " + str(larger) + "\ntotal: " + str(total) + "\n\n")

total = total + smaller


smaller = smaller + 1

print("Variables after incrementing smaller number:\nSmaller: " +


str(smaller) + "\nLarger: " + str(larger) + "\ntotal: " + str(total) + "\n\n")

if smaller > larger:


print ("Now we stop because smaller is no longer less than or equal to
larger, it's now greater.\n Current smaller: " + str(smaller) + "\nCurrent Larger:
" + str(larger) + "\nIn other words the while statement is no longer true\n\n")

print "END OF WHILE LOOP\nTOTAL AFTER WHILE LOOP: " + str(total) + "\n\n\n\n"

#reset for next example


smaller = 0
larger = 0
total = 0

if num1 > num2:


larger = num1
smaller = num2
else :
larger = num2
smaller = num1

# For Loop Explanation: Here we set up a for loop. In simple terms a for loop is
saying

# that for every thing(variable) in a list of things(an array, a set of numbers, a


group)

# an easier example is taking a basket of fruit, say that for every piece of fruit
in this

# basket you want to take one bite, the actual python version of this would be :

# EXAMPLE FOR LOOP:

# below is where it's saying "for every fruit in the basket do this thing"

# for fruit in basket:


# Action = take a bite.

# And this is where you state the action you want to take for every fruit
# For this loop, the "fruit" is the "incrementer" variable and the "basket"

# will be a list of numbers starting from the smaller and going up to what

# the larger number is, we create this "basket" by using range(smaller, larger +1)

# which creates a list of numbers between the smaller and larger numbers (not
inclusively)

# example: Range(1,4) = [1, 2, 3] = a list of numbers between 1 and 4

# Now we state that for every fruit (incrementer) in the basket (list of numbers)

# we are going to take that fruit (incrementer) and add it to the total

# Run the Code for Better Explanation

print "BEGINNING OF FOR LOOP\n\n"

mylist = range(smaller, larger + 1)


print "\nThe 'Basket' = Range(smaller, larger + 1) = ", mylist
for incrementer in range(smaller, larger + 1) :

print "\n\nValue of 'fruit' or incrementer at the momenet: ", incrementer


print "\n\nTotal before adding the incrementer: ", total

total = total + incrementer

print "Total after adding the incrementer: ", total

print "\n\nthis is the total", total

You might also like