Skip to content

Instantly share code, notes, and snippets.

@ansakoy
Last active December 19, 2015 04:09
Show Gist options
  • Save ansakoy/5895141 to your computer and use it in GitHub Desktop.
Save ansakoy/5895141 to your computer and use it in GitHub Desktop.
# 6.189 A Gentle Introduction to Programming
# Mechanical MOOC
# Exercise 1.8
print '*****LOOPS*****'
# Task 1
# prints out decimal equivalents of 1/2, 1/3, 1/4, ..., 1/10
for i in range(2,11):
print 1.0/i
# Task 2
# Countdown from a given positive number to 0
n = input('Enter a positive number: ')
if n > 0:
while n >= 0:
print n
n -= 1
else:
print "Invalid input"
# Task 3
#Calculating exponentials
base = input("Enter base: ")
exp = input("Enter exponent: ")
x = base # Here I place the initial value for calculation
# I use FOR LOOP to set the number of times I want to
# itirate this calculation
for n in range(1, exp):
x *= base # This is the formula of calculation
print x # This shows what's going on in the process
print x
# Testing Andrew's version
base = input("Enter base: ")
exp = input("Enter exponent: ")
x = 1
for n in range(exp):
x *= base
print x
# The same task performed with the help of while loop
# Thanks e.mccormick for brilliant enlightening examples of how it works!
exp = 3 # Set the number of times the base should be multiplied by itself
exponent = exp # Set the value to be printed
base = 5 # Set the base
result = 1
while exp > 0: # The while loop sets the number of iterations
exp = exp - 1
result = result * base
print result # This shows what's going on in the process
print str(base) + ' power ' + str(exponent) + ' equals ' + str(result)
# Task 4
# Making a user enter an integer divisible by 2
n = input('Enter an even integer number: ')
# Making sure the input value is an even integer
while n % 2 != 0:
print "No, it's not even! Even means divisible by 2"
n = input("Enter an EVEN integer: ")
while type(n) == float:
print "I asked to enter integer, didn't I?"
n = input('Enter an even INTEGER number: ')
print "Congrats, you've done it!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment