-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo-5.py
More file actions
94 lines (71 loc) · 2.26 KB
/
Copy pathdemo-5.py
File metadata and controls
94 lines (71 loc) · 2.26 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
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 6 20:00:30 2021
@author: Daniel Mishler
"""
# Exceptions (when things get messy)
# Spaghetti code
# Try-except
# Always ask for permission never forgiveness (use ifs)
# Always ask for forgiveness never permission
try:
quotient = 9/1
except:
quotient = None
print(quotient)
try:
newFile = open("newFile.txt","r")
print(newFile.read())
newFile.close()
except:
print("File not found!")
# Strengths of try-except:
# Doesn't crash code (vital for longevity)
# Extends the ease of use of python (it would be HARDER or SLOWER to do without)
# Weaknesses of try-except:
# HIDES the errors (it takes work to debug)
# try-except is addictive and OFTEN there are better and more readable ways
# (It's like a spice)
# find()
interpretString = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
print(interpretString)
# find() will always return the index of the FIRST match
# If find() returns -1, then the pattern was not found.
# no one said find() had to search for single characters:
# You can search strings.
# split()
# splits a string based on its whitespace
splitMe = "Hello my name is Daniel"
newList = splitMe.split()
print(newList)
# aside: index() is find() for lists
# index() is to lists as find() is to strings.
interpretSplit = interpretString.split('l')
print(interpretSplit)
# By default, splits on all whitespace. Otherwise, splits on your argument.
# Slice
sliceMe = [1,1.1,1.2,1.3,1.4,1.5,1.6]
# sliceMe[a:b]
# a subarray consisting of index a, and all indexes up to but NOT including b.
# If an argument is missing, then go to the end.
def findAC(ACstring):
# Take a string that contains an armor class and return an int (that is AC)
print("converting",ACstring,"to int")
try:
AC = int(ACstring)
except:
ACsplit = ACstring.split()
AC = int(ACsplit[0])
return AC
AC1 = findAC("10")
AC2 = findAC("13 (armor scraps)")
print(AC1,AC2)
def findACBetter(ACstring):
# Take a string that contains an armor class and return an int (that is AC)
print("converting",ACstring,"to int")
ACsplit = ACstring.split()
AC = int(ACsplit[0])
return AC
AC1 = findACBetter("10")
AC2 = findACBetter("13 (armor scraps)")
print(AC1,AC2)