Skip to content

Commit 425d1ed

Browse files
committed
Added some projects
1 parent 0d35c5a commit 425d1ed

File tree

4 files changed

+189
-0
lines changed

4 files changed

+189
-0
lines changed

Calculator/main.py

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
'''
2+
Calculator
3+
-------------------------------------------------------------
4+
'''
5+
6+
7+
import os
8+
9+
10+
def addition():
11+
os.system('cls' if os.name == 'nt' else 'clear')
12+
print('Addition')
13+
14+
continue_calc = 'y'
15+
16+
num_1 = float(input('Enter a number: '))
17+
num_2 = float(input('Enter another number: '))
18+
ans = num_1 + num_2
19+
values_entered = 2
20+
print(f'Current result: {ans}')
21+
22+
while continue_calc.lower() == 'y':
23+
continue_calc = (input('Enter more (y/n): '))
24+
while continue_calc.lower() not in ['y', 'n']:
25+
print('Please enter \'y\' or \'n\'')
26+
continue_calc = (input('Enter more (y/n): '))
27+
28+
if continue_calc.lower() == 'n':
29+
break
30+
num = float(input('Enter another number: '))
31+
ans += num
32+
print(f'Current result: {ans}')
33+
values_entered += 1
34+
return [ans, values_entered]
35+
36+
37+
def subtraction():
38+
os.system('cls' if os.name == 'nt' else 'clear')
39+
print('Subtraction')
40+
41+
continue_calc = 'y'
42+
43+
num_1 = float(input('Enter a number: '))
44+
num_2 = float(input('Enter another number: '))
45+
ans = num_1 - num_2
46+
values_entered = 2
47+
print(f'Current result: {ans}')
48+
49+
while continue_calc.lower() == 'y':
50+
continue_calc = (input('Enter more (y/n): '))
51+
while continue_calc.lower() not in ['y', 'n']:
52+
print('Please enter \'y\' or \'n\'')
53+
continue_calc = (input('Enter more (y/n): '))
54+
55+
if continue_calc.lower() == 'n':
56+
break
57+
num = float(input('Enter another number: '))
58+
ans -= num
59+
print(f'Current result: {ans}')
60+
values_entered += 1
61+
return [ans, values_entered]
62+
63+
64+
def multiplication():
65+
os.system('cls' if os.name == 'nt' else 'clear')
66+
print('Multiplication')
67+
68+
continue_calc = 'y'
69+
70+
num_1 = float(input('Enter a number: '))
71+
num_2 = float(input('Enter another number: '))
72+
ans = num_1 * num_2
73+
values_entered = 2
74+
print(f'Current result: {ans}')
75+
76+
while continue_calc.lower() == 'y':
77+
continue_calc = (input('Enter more (y/n): '))
78+
while continue_calc.lower() not in ['y', 'n']:
79+
print('Please enter \'y\' or \'n\'')
80+
continue_calc = (input('Enter more (y/n): '))
81+
82+
if continue_calc.lower() == 'n':
83+
break
84+
num = float(input('Enter another number: '))
85+
ans *= num
86+
print(f'Current result: {ans}')
87+
values_entered += 1
88+
return [ans, values_entered]
89+
90+
91+
def division():
92+
os.system('cls' if os.name == 'nt' else 'clear')
93+
print('Division')
94+
95+
continue_calc = 'y'
96+
97+
num_1 = float(input('Enter a number: '))
98+
num_2 = float(input('Enter another number: '))
99+
while num_2 == 0.0:
100+
print('Please enter a second number > 0')
101+
num_2 = float(input('Enter another number: '))
102+
103+
ans = num_1 / num_2
104+
values_entered = 2
105+
print(f'Current result: {ans}')
106+
107+
while continue_calc.lower() == 'y':
108+
continue_calc = (input('Enter more (y/n): '))
109+
while continue_calc.lower() not in ['y', 'n']:
110+
print('Please enter \'y\' or \'n\'')
111+
continue_calc = (input('Enter more (y/n): '))
112+
113+
if continue_calc.lower() == 'n':
114+
break
115+
num = float(input('Enter another number: '))
116+
while num == 0.0:
117+
print('Please enter a number > 0')
118+
num = float(input('Enter another number: '))
119+
ans /= num
120+
print(f'Current result: {ans}')
121+
values_entered += 1
122+
return [ans, values_entered]
123+
124+
125+
def calculator():
126+
quit = False
127+
while not quit:
128+
results = []
129+
print('Simple Calculator in Python!')
130+
print('Enter \'a\' for addition')
131+
print('Enter \'s\' for substraction')
132+
print('Enter \'m\' for multiplication')
133+
print('Enter \'d\' for division')
134+
print('Enter \'q\' to quit')
135+
136+
choice = input('Selection: ')
137+
138+
if choice == 'q':
139+
quit = True
140+
continue
141+
142+
if choice == 'a':
143+
results = addition()
144+
print('Ans = ', results[0], ' total inputs: ', results[1])
145+
elif choice == 's':
146+
results = subtraction()
147+
print('Ans = ', results[0], ' total inputs: ', results[1])
148+
elif choice == 'm':
149+
results = multiplication()
150+
print('Ans = ', results[0], ' total inputs: ', results[1])
151+
elif choice == 'd':
152+
results = division()
153+
print('Ans = ', results[0], ' total inputs: ', results[1])
154+
else:
155+
print('Sorry, invalid character')
156+
157+
158+
if __name__ == '__main__':
159+
calculator()

Calculator/readme

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
As one of the easy Python projects, this program creates a basic calculator application with addition, subtraction, multiplication, and division functions.
2+
3+
This is one of the Python practice projects that are great for learning how to use loops, functions, conditional statements, user input, and string formatting. We’ve also used the Python os module to clear the screen after the user completes their calculation actions.

Pascal's Triangle/main.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'''
2+
Pascal's Triangle
3+
-------------------------------------------------------------
4+
Number of combinations via "n choose k" or nCk = n! / [k! * (n-k)!]
5+
'''
6+
7+
8+
from math import factorial
9+
10+
11+
def pascal_triangle(n):
12+
for i in range(n):
13+
for j in range(n-i+1):
14+
print(end=' ')
15+
16+
for j in range(i+1):
17+
18+
print(factorial(i)//(factorial(j)*factorial(i-j)), end=' ')
19+
20+
print()
21+
22+
23+
if __name__ == '__main__':
24+
pascal_triangle(5)

Pascal's Triangle/readme

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This Python project prints out Pascal’s Triangle by utilizing conditional statements and loops. It also uses the standard library’s math module and factorial function to evaluate the ‘number of combinations’ equation used to generate the values in the triangle.
2+
3+
Experiment with the seed number for the triangle to examine how the ‘combinations’ equation is used to generate successive values in the triangle.

0 commit comments

Comments
 (0)