Conditionals and Python
My hacks from this week (week 1)
# prints a statement based on a simple if function
a = 1
b = 1
if (a == b):
print("A equals B")
A equals B
# sums all numbers from 1 to 100
import time
x = list(range(1,101)) # This takes all numbers 1-100
print('Adding all numbers 1-100...')
time.sleep(1.5) # waits for for 1.5 seconds
print('=',sum(x))
time.sleep(1) # waits for for 1 second
print('Sum complete.')
Adding all numbers 1-100...
= 5050
Sum complete.
# sums all numbers between given user input
import time
print('Add all numbers between any two given values')
x = int(input('What is the smaller number?\n'))
y = int(input('What is the larger number?\n'))
z = list(range(x,y+1)) # creates a list from which to add all numbers
time.sleep(.7) # waits for for .7 seconds
print('The sum of the numbers between',x,'and',y,'is',sum(z))
print('Sum complete.')
Add all numbers between any two given values
The sum of the numbers between 4 and 7 is 22
Sum complete.
# sums all numbers between given user input including numbers in descending value
# however those in descending value will output the opposite (negative) of the true sum
print('Add all numbers between any two given values')
x = int(input('What is your first number?\n'))
y = int(input('What is your second number?\n'))
if x > y: # to fix any error in user output causing
start = y
end = x
else:
start = x
end = y
z = list(range(start, end+1)) # adding 1 to y to get range of numbers including y
print('The sum of the numbers between these two values =',sum(z))
print('Sum complete.')
Add all numbers between any two given values
The sum of the numbers between these two values = 22
Sum complete.
# lists all numbers between two given values
w = int(input('What is your first number?\n'))
y = int(input('What is your second number?\n'))
if w > y:
print('Please input numbers in ascending order')
elif w == y:
print('No range! These are the same number')
while w < y:
print (w)
w += 1
if w > y:
break
elif w == y:
print (y)
break
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Coding Definitions
code block: code which is grouped together
sequence: the order in which commands are executed by a computer
selection: points where a program branches off into different steps depending on some conditional. also known as an ‘if statement’
iteration: a function that repeats a specific code block until a specified result occured