-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonMiniPrograms.py
More file actions
146 lines (127 loc) · 4.59 KB
/
PythonMiniPrograms.py
File metadata and controls
146 lines (127 loc) · 4.59 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
################################
# Author: Rahim Siddiq
# Python Mini-Programs
################################
### Problem 1 ###
# User input #
n = int(input("Please enter a number:"))
# Variable for initial count #
count = 0
# Conditional loop range 1-User input #
for i in range(1, n):
# Print using end= to keep output in line and str "," to separate #
print(i, end = ",")
# Increment count #
count = count + i
# Conditional to print 10 values per row #
if i % 10 == 0:
print()
# Sum of variable once outside of loop #
count = count + n
# print final value for n #
print(n)
# print total #
print("Total =", (count))
### Problem 2 ###
# User Input #
n = int(input("Please enter a number:"))
# Conditional based on user input #
for i in range(n):
# Inside Loop to make j run i number of times #
for j in range(n):
# print stars and leave a space, end= to keep in same line #
print("*", end = "")
# Once inside loop prints stars, print line numbers #
print("", i + 1)
# Print the number of stars #
print("You displayed", (n * n), "stars.")
### Problem 3 ###
# User input #
n = int(input("Enter the max number of stars per line")) # User input
# Conditional to print stars in ascending order from 1 to user input #
for i in range(1, n + 1):
# Nested loop to print stars in increment #
for j in range(1, i + 1):
# Print stars and format on the same line with end= followed by a space #
print("*", end = "")
# Print the number of the line #
print("", i)
# Conditional to print stars in descending order from user input -1 to 0 in negative increment #
for i in range(n - 1, 0, -1):
# Nested loop to print stars #
for j in range(0, i):
# Print stars on same line with end= followed by a space #
print("*", end = "")
# Print the line number #
print("", i)
### Problem 4 ###
# User Input #
n = int(input("Please enter max number of starts per line:"))
print("Part I")
# From 1 to range of user input +1 so its input inclusive #
for i in range(1, n + 1):
# Nested loop to print starts incrementally #
for j in range(1, i + 1):
# Print starts j times using end= to list on the same line and space #
print(i, end = "")
# Print space, value of i end= to keep in line \n for new line next time through loop #
print(" ", i, end = "\n")
# From user input -1 to 0 in negative increment #
for i in range(n - 1, 0, -1):
# Loop to print in negative increment #
for j in range(i, 0, -1):
# Print i j many times end= to keep on same line then a space #
print(i, end = "")
# Print space value of i end= keep line and call for a new line for next loop #
print(" ", i, end = "\n")
print("Part II")
# Loop for inverse, from user input to 0 decrement by 1 #
for i in range(n, 0, -1):
# Nest loop to print starts #
for j in range(i, 0, -1):
# Print i j number of times decrementally #
print(i, end = "")
# Print value of i end= to keep line and \n to call new line
print(" ", i, end = "\n")
# Similar to start loop except range at 2 to avoid double input from previous loop #
for i in range(2, n + 1):
for j in range(1, i + 1):
print(i, end = "")
print(" ", i, end = "\n")
### Problem 5 ###
# Def Function #
def calculateParkingFee(count, hr):
# Flat Rate #
parkingfee = 3
# Conditional loop for hours past 3 #
if hr > 3:
# Subtract flat rate #
additional = hr - 3
# Add a dollar for each hour over flat rate #
parkingfee = parkingfee + (additional * 1)
# Conditional for parking over 12 hours #
if parkingfee > 12:
# Flat rate 12 #
parkingfee = 12
# Print values and \t for spacing to make tabular then return function #
print(count, "\t" * 2, hr, "\t" * 2, parkingfee)
return parkingfee
# User input for number of cars #
numcars = int(input("Enter the number of cars:"))
carhours = []
# Loop from 0 to user input number of cars #
for i in range(0, numcars):
print("Enter the hours of car #:")
# User input for number of hours parked #
hr = int(input("Enter number of hours parked:"))
# Append hours to carhours so we can call value in list #
carhours.append(hr)
# Print headers for table #
print("Customer", "\t", "Hours", "\t" * 2, "Fee")
# Assignment variable for total #
totalfee = 0
# Loop to calculate total #
for i in range(0, numcars):
totalfee = totalfee + calculateParkingFee(i + 1, int(carhours[i]))
# Print str tab 3x to bring in line with fee and print total cost of fee #
print("Total Fee", "\t" * 3, totalfee)