Tuesday, October 10, 2017

More strings and peer exercise 4

Here are the string practices complete with errors

Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> 
======== RESTART: C:/Users/sconger/Documents/Python Programs/vans.py ========
You will need 4 Vans
>>> 
======== RESTART: C:/Users/sconger/Documents/Python Programs/vans.py ========
You will have 3 full vans
You will have one van with 5 Passengers
You will need 4 Vans
>>> #string is list of characters
>>> myString="the quick fox jumped over the lazy brown dog"
>>> print(myString[10])
f
>>> print(len(myString))
44
>>> for i range(0,len(myString)-1)
SyntaxError: invalid syntax
>>> for i range (0, len(myString)-1):
 
SyntaxError: invalid syntax
>>> for i in range (0, len(myString)-1):
 print(myString[i])

t
h
e
 
q
u
i
c
k
 
f
o
x
 
j
u
m
p
e
d
 
o
v
e
r
 
t
h
e
 
l
a
z
y
 
b
r
o
w
n
 
d
o
>>> for i in range (0, len(myString)):
 print(myString[i])

t
h
e
 
q
u
i
c
k
 
f
o
x
 
j
u
m
p
e
d
 
o
v
e
r
 
t
h
e
 
l
a
z
y
 
b
r
o
w
n
 
d
o
g
>>> print (myString[10,13])
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    print (myString[10,13])
TypeError: string indices must be integers
>>> print (myString[10:13])
fox
>>> wordList = myString.split()
>>> for str in wordList:
 print(str)

the
quick
fox
jumped
over
the
lazy
brown
dog
>>> print (len(wordList))
9
>>> print(wordList[3])
jumped
>>> 
======== RESTART: C:/Users/sconger/Documents/Python Programs/peer5.py ========
enter a sentenceIt was an existential crises for many
the average word is  4.428571428571429 characters long
>>> 
======== RESTART: C:/Users/sconger/Documents/Python Programs/peer5.py ========
enter a sentence a to the four fives
the average word is  3.0 characters long
>>> 

Here is the peer exercise 5

#peer exercise 5

def main():
    mySentence = input("enter a sentence")
    wordList = mySentence.split()
    counter=0
    for i in range(0,len(wordList)):
        wordLength=len(wordList[i])
        #counter=counter + wordLength
        counter += wordLength
    average = counter/len(wordList)
    print ("the average word is ",average,"characters long")

main()

No comments:

Post a Comment