Advertisement
ansakoy

Loops

Jun 30th, 2013
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.24 KB | None | 0 0
  1. # 6.189 A Gentle Introduction to Programming
  2. # Mechanical MOOC
  3. # Exercise 1.8
  4. print '*****LOOPS*****'
  5.  
  6. # Task 1
  7. # prints out decimal equivalents of 1/2, 1/3, 1/4, ..., 1/10
  8.  
  9. for i in range(2,11):
  10.     print 1.0/i
  11.  
  12. # Task 2
  13. # Countdown from a given positive number to 0
  14.  
  15. n = input('Enter a positive number: ')
  16. if n > 0:
  17.     while n >= 0:
  18.         print n
  19.         n -= 1
  20. else:
  21.     print "Invalid input"
  22.  
  23. # Task 3
  24. #Calculating exponentials
  25.  
  26. base = input("Enter base: ")
  27. exp = input("Enter exponent: ")
  28. x = base # Here I place the initial value for calculation
  29.  
  30. # I use FOR LOOP to set the number of times I want to
  31. # itirate this calculation
  32. for n in range(1, exp):
  33.     x *= base # This is the formula of calculation
  34.     print x # This shows what's going on in the process
  35. print x
  36.  
  37. # Task 4
  38. # Making a user enter an integer divisible by 2
  39.  
  40. n = input('Enter an even integer number: ')
  41. # Making sure the input is an even integer
  42. while n % 2 != 0:
  43.         print "No, it's not even! Even means divisible by 2"
  44.         n = input("Enter an EVEN integer: ")
  45.         while type(n) == float:
  46.             print "I asked to enter integer, didn't I?"
  47.             n = input('Enter an even INTEGER number: ')
  48. print "Congrats, you've done it!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement