Chapter 5: Character Input/Output and String Functions

C22INI, Page number:452

In [1]:
#Get input
print "What is your first initial?"
initial=raw_input()

#Check if it is an alphabet
while not initial.isalpha():
    print "That was not a valid initial!"
    print "What is your first initial?"
    initial=raw_input()
print "Thanks"
What is your first initial?
p
Thanks

C22GB, Page number:454

In [2]:
#Get input
#ans=raw_input("Are you a girl or a boy (G/B)? ")  
ans="b"
#convert answer to uppercase
ans=ans.upper()   
if ans=='G':
    print "You look pretty today!\n"
else:
    if ans=='B':
        print "You look handsome today!\n"
    else:
        print "Your answer makes no sense!\n"
You look handsome today!

C22GPS1, Page number:459

In [3]:
#get book title
#book=raw_input("What is the book title? ")  
book="Mary and Her Lambs"
#print book title
print book          
print "Thanks for the book!\n"
Mary and Her Lambs
Thanks for the book!

C22ABS, Page number:463

In [4]:
#Get input
#age1=input("\nWhat is the first child's age? ")
#age2=input("\nWhat is the second child's age? ")
age1=10
age2=12
diff=age1-age2               
# abs() function determines absolute value
diff=abs(diff)              
#Result
print "\nThey are ",diff," years apart."
They are  2  years apart.