Tuesday, December 26, 2017

Generate numbers from 1-5

Ther are many ways to print the first five integer numbers. I’m going to give you three different ways to make this happen:

  • In the first method, we use “for” with “range”: “i” will begin empty and obtain different values every time “i” gets a new value it will be printed.
  • For the second method, we again use “for” but the maximum range for “i” is 5 and every time we print “i” we add one.
  • For the third example, we use “while” so while “j” is smaller than 6 print “j” and ad 1 to “j”

To make the separation on the different examples I used two different sorts of methods: In the first, there isn't really much to it, just print a bunch of underscores. But in my second I used this, “print("_" * 40) print("\n")”, at first I print 40 times “_” but I could have chosen any sort of symbol, after that I ad “print(“\n”)” wich only leaves a small sapace after each line 

The code:

 # Generate numbers from 1-5  
 #Method 1. With For  
 for i in range(1, 6):  
 #Method 2. With For (A variant)  
 for i in range(5): #var i takes the values from 0 to 4  
 #Method 3. With While  
 print("_" * 40)  
 while (j<6 ):  
   j =j+ 1     # j += 1  

The terminal:

No comments:

Post a Comment