-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCH.py
444 lines (394 loc) · 12.6 KB
/
CH.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import turtle
from time import sleep
count = 1
# TESTING X - NAME X - NAME
def alkyne(direction):
if direction == 'Up':
turtle.penup()
turtle.forward(5)
turtle.right(90)
turtle.forward(5)
turtle.pendown()
turtle.forward(40)
turtle.left(180)
turtle.penup()
turtle.forward(45)
turtle.left(90)
turtle.forward(10)
turtle.left(90)
turtle.forward(5)
turtle.pendown()
turtle.forward(40)
turtle.right(180)
turtle.penup()
turtle.forward(45)
turtle.right(90)
turtle.forward(5)
turtle.pendown()
elif direction == 'Down':
turtle.penup()
turtle.forward(5)
turtle.left(90)
turtle.forward(5)
turtle.pendown()
turtle.forward(40)
turtle.left(180)
turtle.penup()
turtle.forward(45)
turtle.right(90)
turtle.forward(10)
turtle.right(90)
turtle.forward(5)
turtle.pendown()
turtle.forward(40)
turtle.right(180)
turtle.penup()
turtle.forward(45)
turtle.left(90)
turtle.forward(5)
turtle.pendown()
def alkene(direction):
if direction == 'Up':
turtle.penup()
turtle.forward(5)
turtle.right(90)
turtle.forward(5)
turtle.pendown()
turtle.forward(40)
turtle.left(180)
turtle.penup()
turtle.forward(45)
turtle.left(90)
turtle.forward(5)
turtle.left(180)
turtle.pendown()
elif direction == 'Down':
turtle.penup()
turtle.forward(5)
turtle.left(90)
turtle.forward(5)
turtle.pendown()
turtle.forward(40)
turtle.left(180)
turtle.penup()
turtle.forward(45)
turtle.right(90)
turtle.forward(5)
turtle.right(180)
turtle.pendown()
def sus_len(sus_len, direction, count=0):
if direction == 'Up':
turtle.left(45)
elif direction == 'Down':
turtle.right(45)
count = 1
for i in range(sus_len):
if count == sus_len: # this break only here since this for is the foward statement
print('first brek')
break
turtle.forward(50)
count += 1
turtle.right(90)
if count == sus_len: # this break only here since this for is the foward statement
break
turtle.forward(50)
count += 1
turtle.left(90)
turtle.forward(50)
turtle.left(180)
if sus_len % 2 == 1:
print('retocede')
for i in range(count): # going back statement
if count == 1:
break
turtle.forward(50)
count -= 1
turtle.right(90)
if count == 1:
break
turtle.forward(50)
count -= 1
turtle.left(90)
turtle.forward(50)
if direction == 'Up':
turtle.left(135)
if direction == 'Down':
turtle.right(135)
elif sus_len % 2 == 0:
print('xx')
for i in range(count): # going back statement
if count == 1:
break
turtle.forward(50)
count -= 1
turtle.left(90)
if count == 1:
break
turtle.forward(50)
count -= 1
turtle.right(90)
turtle.forward(50)
if direction == 'Up':
turtle.left(135)
if direction == 'Down':
turtle.right(135)
def straight(x):
if x == 'Up':
turtle.left(90)
turtle.forward(50)
if x == 'Down':
turtle.right(90)
turtle.forward(50)
# this function is to attach the sus on the main chain
# the main chain function has the message for invalid sus_loc
def ch(number, sus_loc=0, sus_type=0, sus_loc_nd=0, ene=0, yne=0, count=1):
# ch(5, 2, 1, 0, 3)
down = 'Down'
up = 'Up'
if number == 1:
turtle.left(90)
return None
if ene >= number:
print('Double bond location cannot start at last carbon in chain')
return None
if yne == number:
print(f'triple bond location cannot start at last carbon in chain')
return None
if sus_loc != 0 and sus_loc == yne or sus_loc != 0 and sus_loc == yne + 1:
print('Not valid. Substituent must not start where triple bond starts or finishes')
return None
# this is to avoid changing chain size, if sus_len to large chain name changes
if sus_type > 0: # > 0 because if no substituents no need to check
if sus_type >= sus_loc:
print('1Invalid, Substituent will change the chain name.\nSubstituent position and #C on substitutent must keep main chain name as entered')
return None
if sus_loc_nd > 0:
if sus_type >= sus_loc_nd:
print('2Invalid, Substituent will change the chain name.\nSubstituent position and #C on substitutent must keep main chain name as entered')
return None
if sus_type > number - sus_loc or sus_type > number - sus_loc_nd:
print('Invalid, Substituent will change the chain name.\nSubstituent position and #C on substitutent must keep main chain name as entered')
return None
print(f'{number} carbons in main chain')
print(f'{sus_type} carbons in Substituent')
print(f'{sus_loc} = first Substituent location')
print(f'{sus_loc_nd} = second Substituent location')
print(f'{ene} = double bond location')
print(f'{yne} = triple bond location')
# for number of Carbons do up and down until number of carbons == count
for i in range(number):
if count == ene:
alkene(down)
if count == yne:
alkyne(down)
# if C number is reached
if count == number:
turtle.shape('circle')
turtle.shapesize(0.5)
break
straight(up)
count += 1
if sus_loc == count:
sus_len(sus_type, up)
if sus_loc_nd == count:
sus_len(sus_type, up)
for n in range(number):
if count == ene:
alkene(up)
if count == yne:
alkyne(up)
# for number of C
if count == number:
turtle.shape('circle')
turtle.shapesize(0.5)
break
straight(down)
count += 1
# start sus creation in position = count
if sus_loc == count:
sus_len(sus_type, down)
if sus_loc_nd == count:
sus_len(sus_type, down)
break
def name_check(x):
if met in x:
return 1
elif et in x:
return 2
elif prop in x:
return 3
elif but in x:
return 4
elif pent in x:
return 5
elif hex in x:
return 6
elif hept in x:
return 7
elif oct in x:
return 8
elif non in x:
return 9
elif dec in x:
return 10
elif ico in x:
return 20
def number_of_carbons_on_main_chaing(x):
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18',
'19', '20']
# if the name is just a simple chain
prefix = ['Met', 'Eth', 'Pro', 'But', 'Pen', 'Hex', 'Hep', 'Oct', 'Non', 'Dec',
'Ico'] # fix this part because we are supposed to use vars not strings
print(f'Length = {len(name)}')
if len(name) == 1:
if yl in name[0]:
print('Main chain cant be Substituent')
elif ane in name[0]:
ch(x)
elif ene in name[0]:
ch(x, 0, 0, 0, 1)
elif yne in name[0]:
ch(x, 0, 0, 0, 0, 1)
else:
print(f'Not valid')
# elif:
# ch(x, 0, 0, 0, yne)
# print(f'If no substitutient, Carbons chain must be termination "ane"\nElse specify the double or triple bond as X-{name[0]}')
elif len(name) == 2:
# this is in case the input is x-methene/yne
if met in name[1]: # 3 methene
print('Methane is a CH4, no option for x-METHene/yne')
elif ene in name[-1]: # 2 pentene
ch(name_check(name[-1]), 0, 0, 0, int(name[-2]))
elif yne in name[-1]: # 2 pentene
ch(name_check(name[-1]), 0, 0, 0, 0, int(name[-2]))
else:
print(f'Termination must be "ene/yne"\nOtherwise enter only the name of a simple C chain')
# if the name has 3 characters on it must be sus location-susType-chain#
elif len(name) == 3:
if met in name[-1]:
print('Methane is a CH4, no option for x-METHene/yne')
elif ane in name[-1]:
if name[-3] == numbers[0]:
print('Substituent location cannot be 0 nor 1')
elif name[-3] == numbers[0]:
print('*Substituent location cannot be 0 nor 1')
else:
ch(x, int(name[-3]), name_check(name[-2]))
# ene no location given
elif ene in name[-1] and name[-2][0:3] in prefix:
ch(x, int(name[-3]), name_check(name[-2]), 0, 1)
elif yne in name[-1] and name[-2][0:3] in prefix:
ch(x, int(name[-3]), name_check(name[-2]), 0, 0, 1)
elif len(name) == 4:
if met in name[-1]:
print('Methane is a CH4, no option for x-METHene/yne')
# ex: 2 - 3 methyl pentane
elif ane in name[-1]:
if name[-4] not in numbers or name[-3] not in numbers or 'yl' not in name[-2]:
print('Invalid: number-number-Substituent-chain accepted only')
elif name[-2][0:3] in prefix:
print('2 times Substituent')
ch(x, int(name[-3]), name_check(name[-2]), int(name[-4]))
else:
print('Invalid: number-number-Substituent-chain accepted only')
# ex: 3 methyl 4 pentene
elif ene in name[-1]:
if name[-2] in numbers:
ch(name_check(name[-1]), int(name[-4]), name_check(name[-3]), 0, int(name[-2]))
# ex: 4 methyl 5 hexyne
elif yne in name[-1]:
if name[-2] in numbers:
ch(name_check(name[-1]), int(name[-4]), name_check(name[-3]), 0, 0, int(name[-2]))
# print('please input the name of the molecule in format\nnumber-substituentName-chain(ending in ane only)\nor\nnumber-number-(prefix)substituentName-chain(ending in ane only)')
molecule = input(': ')
name = []
numbers = range(1, 20)
dash = '-'
comma = ','
if dash in molecule:
molecule = molecule.replace('-', ' ')
if comma in molecule:
molecule = molecule.replace(',', '')
molecule_split = molecule.split()
for i in molecule_split:
name.append(i.capitalize())
print(name)
print(f'Main chain = {name[-1]}')
met = 'Meth'
et = 'Eth'
prop = 'Prop'
but = 'But'
pent = 'Pent'
hex = 'Hex'
hept = 'Hept'
oct = 'Oct'
non = 'Non'
dec = 'Dec'
undec = 'Undec'
dodec = 'Dodec'
hexdec = 'hexadec'
octadec = 'octadec'
ico = 'Ico'
##
yl = 'yl'
ane = 'ane'
ene = 'ene'
yne = 'yne'
left = 'left'.capitalize()
right = 'right'.capitalize()
screen = turtle.Screen()
screen.bgcolor('gray')
screen_x = 800
screen_y = 600
screen.setup(screen_x, screen_y)
# turtle.hideturtle()
turtle.penup()
turtle.shape()
turtle.shapesize(1.5)
turtle.goto(-screen_x / 2 + 50, 0)
turtle.pensize(2)
turtle.speed(5)
turtle.pendown()
turtle.right(45)
# print(f'Turtle pos = {turtle.pos()}')
sleep(2)
try:
if met in name[-1]:
number_of_carbons_on_main_chaing(1)
elif et in name[-1]:
number_of_carbons_on_main_chaing(2)
elif prop in name[-1]:
number_of_carbons_on_main_chaing(3)
elif but in name[-1]:
number_of_carbons_on_main_chaing(4)
elif pent in name[-1]:
number_of_carbons_on_main_chaing(5)
elif hex in name[-1]:
number_of_carbons_on_main_chaing(6)
elif hept in name[-1]:
number_of_carbons_on_main_chaing(7)
elif oct in name[-1]:
number_of_carbons_on_main_chaing(8)
elif non in name[-1]:
number_of_carbons_on_main_chaing(9)
elif dec in name[-1]:
number_of_carbons_on_main_chaing(10)
# i am not activating this because undec has dec so it will recognized as 10 not 11, i still need to fix this
# if undec in name[-1]:
# number_of_carbons_on_main_chaing(11)
#
# if dodec in name[-1]:
# number_of_carbons_on_main_chaing(12)
#
# if hexdec in name[-1]:
# number_of_carbons_on_main_chaing(16)
#
# if octadec in name[-1]:
# number_of_carbons_on_main_chaing(18)
elif ico in name[-1]:
number_of_carbons_on_main_chaing(20)
except TypeError:
print('Double check if correctly written')
turtle.hideturtle()
screen.mainloop()