Open main menu

CDOT Wiki β

Changes

OPS435 Python3 Lab 4

3 bytes removed, 14:26, 5 February 2020
m
PART 2 - Searching and Validating
offset = 0
length = len(text)
while offset < length:
print(offset, text[offset])
offset = offset + 1
if __name__ == '__main__':
s1 = 'Seneca'
print(s1,'contains letter s? ->',find(s1,'s')) print(s1,'contains letter S? ->',find(s1,'S'))
</source> The find() function defined above, is not needed, as it can be replaced by the python keyword '''in''', which can be used as a boolean operator. This boolean operator takes two strings, and return '''True''' if the first appears as a substring in the second.<br/>
'''Create a Python script to validate user input'''
:'''Perform the following instructions'''
:# Create the '''~/ops435/lab4/lab4e.py ''' script. The purpose of this script is to define a function called is_digits(), which takes a string object as its argument and return True if all the characters in the string are all digits, i.e 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9; return False if any one of the characters is not a digit.
:# Use the following template to get started:<source lang="python">
#!/usr/bin/env python3
if __name__ == '__main__':
test_list = ['x1234x3058','12x343058','12348503x','1234x8503']
for item in test_list:
if is_digits(item):
:::'''Sample run 1:'''<source lang="python">
./lab4e.py
x1234 x3058 is not an integer.12x34 3058 is not an integer.1234 8503x is an not integer.1234x 8503 is not an integer.
</source>
:::'''Sample run 2 (with import):'''<source lang="python">
import lab4e
print(is_digits('5665'))
# will output True
print(is_digits('1F'))
14
edits