Some simple counters and totals
This commit is contained in:
		
							parent
							
								
									63d4fcdc15
								
							
						
					
					
						commit
						6c4e660819
					
				
							
								
								
									
										28
									
								
								Python/016_counters.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								Python/016_counters.py
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,28 @@
 | 
			
		|||
# A simple example of counters, subtotals, and totals
 | 
			
		||||
 | 
			
		||||
# A simple loop that counts to 10
 | 
			
		||||
loop_counter = 1
 | 
			
		||||
print("Let's count to 10!")
 | 
			
		||||
print("  ", end='')
 | 
			
		||||
while loop_counter <= 10:
 | 
			
		||||
    print(f" {loop_counter}", end='')
 | 
			
		||||
    loop_counter += 1
 | 
			
		||||
print()
 | 
			
		||||
 | 
			
		||||
# Let's add up all the even numbers lower than 100
 | 
			
		||||
even_total = 0
 | 
			
		||||
span = range(0, 101, 2)
 | 
			
		||||
for num in span:
 | 
			
		||||
    even_total += num
 | 
			
		||||
print(f"The sum of all even numbers between 0 and 100 is {even_total}")
 | 
			
		||||
 | 
			
		||||
# Let's add up all the even numbers lower than 100
 | 
			
		||||
odd_total = 0
 | 
			
		||||
span = range(1, 101, 2)
 | 
			
		||||
for num in span:
 | 
			
		||||
    odd_total += num
 | 
			
		||||
print(f"The sum of all odd numbers between 0 and 100 is {odd_total}")
 | 
			
		||||
 | 
			
		||||
# Now we total them all up
 | 
			
		||||
total = odd_total + even_total
 | 
			
		||||
print(f"The sum of all numbers between 0 and 100 is {total}")
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user