-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriangles.py
127 lines (107 loc) · 3.26 KB
/
triangles.py
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
# File: triangles.py
# Author: Simon Chu
# Date: April 12, 2017
# Purpose: Draw Triangles.
def getInputs(result):
baseSize = 0
char = ''
indent = 0
print("Please choose an option: ")
print(" " * 7, "1, for triangle pointing up; ")
print(" " * 7, "2, for triangle pointing down; ")
print(" " * 7, "3, to print the result; ")
print(" " * 7, "4, to add a blank line; ")
print(" " * 7, "5, to stop this program; ")
n = input("--> ")
p = n
print() # for turnin
if n == '0':
print("Error: option # must in the range from 1 to 5. ")
n = 0
print()
# determine whether the input is a valid number
try:
p = float(p)
except ValueError:
print("Error: option # must be a valid number, not \""
+ str(n) + "\".")
n = 0
print()
# determine whether the input is an integer
if float(n) != int(float(n)) and n != 0:
print("Error: option # must be a integer, not \""
+ str(n) + "\".")
n = 0
print()
if n == str(n) and n != 0:
n = int(float(n))
if int(float(n)) > 5 or int(float(n)) < 1:
print("Error: option # must in the range from 1 to 5, "
"not \""+ str(n) +"\".")
n = 0
print()
# execute corresponding command
if n == 3:
printTriangle(n,result)
result = ''
elif n == 4:
result = result + "\n"
elif n == 5:
print("The program has stopped. ")
print()
exit()
elif n != 0:
baseSize = int(input("Enter size of base: "))
# change baseSize to next odd number if it is even
if baseSize % 2 == 0:
baseSize = baseSize + 1
print() # for turnin
char = input("Enter character used to draw: ")
char = char[0]
print() # for turnin
indent = int(input("Enter number of blanks preceding \
each line: "))
print() # for turnin
return n,baseSize,char,indent,result
def makeStripe(x,char,result):
result = result + x * (char)
return result
def makeIndent(y,indent,result):
result = result + "\n" + (indent + y) * ' '
return result
def makeTriangle(n,baseSize,indent,char,result):
if n == 1:
x = 1
y = baseSize // 2
for y in range(baseSize // 2, -1, -1):
result = makeIndent(y,indent,result)
result = makeStripe(x,char,result)
x = x + 2
print()
if n == 2:
x = baseSize
y = 0
for x in range(baseSize, 0, -2):
result = makeIndent(y,indent,result)
result = makeStripe(x,char,result)
y = y + 1
print()
return result
def printTriangle(n,result):
if n == 3:
print(result)
def main():
print()
print("Program to draw triangles.")
print("Written by Simon Chu.")
print()
result = ''
n = 0
x = 0
y = 0
while n != 5:
n,baseSize,char,indent,result = getInputs(result)
result = makeTriangle(n, baseSize, indent, char, result)
printTriangle(n,result)
main()
# determine whether the number entered is in the valid interval & print erro