Simple Problems: Caps and Commas

K

Kyle E

I wrote a program that asks a user questions and records the answers and
prints them out at the end. Pretty simple... but I have a few things that I
don't like about it.
----------------------------------------------
print "Do you have a P.O. Box?"
poan = raw_input ("> ")
if poan == "yes":
print "What is your P.O. Box number?"
pobox = input ("> ")
----------------------------------------------
When someone answers "Yes" with cap first letter it determines that "yes"
does not equal "Yes" what are some possible ways to resolve this issue?
======================================
Secondly I'm having a problem with listing information after all user input
"strings" are assigned to various words.

I tried this:
-----------------------------------------------
print "Name: ", fname, mname, lname
print "Address: ",saddy, ",",city, ",",state, ",",zip
-----------------------------------------------

But I get extra spaces after each value:
"Name: Firstname , Middlename , Lastname"
"Address: Street Address , City , State , Zip"

When it should look like this:
"Name: Firstname, Middlename, Lastname"
"Address: Street Address, City, State, Zip"
 
K

KefX

For case-insensitive string comparisons, just make sure both strings are
lowercase (uppercase works too, of course). You're checking against a string
literal, so you only need to make the other one lowercase. Change your 'if'
statement to this:

if string.lower(poan) == "yes":

And at the top of your module, put this:
import string

The library reference is very handy. Use it! :)
I tried this:
-----------------------------------------------
print "Name: ", fname, mname, lname
print "Address: ",saddy, ",",city, ",",state, ",",zip
-----------------------------------------------

But I get extra spaces after each value:
"Name: Firstname , Middlename , Lastname"
"Address: Street Address , City , State , Zip"

When it should look like this:
"Name: Firstname, Middlename, Lastname"
"Address: Street Address, City, State, Zip"

That's because you put extra spaces in the string. Don't do that, and it will
look right.

- Kef
 
B

Bjorn Pettersen

I wrote a program that asks a user questions and records the answers
and prints them out at the end. Pretty simple... but I have a few
things that I don't like about it.
----------------------------------------------
print "Do you have a P.O. Box?"
poan = raw_input ("> ")
if poan == "yes":
print "What is your P.O. Box number?"
pobox = input ("> ")

# lower() is a string method, the in operator tests for list membership
if poan.lower() in ['yes', 'y', 'yup', 'ja']:
...
======================================
Secondly I'm having a problem with listing information after all user
input "strings" are assigned to various words.

I tried this:

# the % operator does 'string interpolation' and gives you more control
# over the outputstring
print 'Name: %s, %s, %s' % (fname, mname, lname)

hth,
-- bjorn
 
K

KefX

# lower() is a string method, the in operator tests for list membership
if poan.lower() in ['yes', 'y', 'yup', 'ja']:

Yeah, that's probably better than my suggestion of string.lower(poan)...I
should have checked to see if that's a string method first.
# the % operator does 'string interpolation' and gives you more control
# over the outputstring
print 'Name: %s, %s, %s' % (fname, mname, lname)

This is just overkill. No "string interpolation" is needed here, just the
programmer inserted extra spaces and didn't expect the print statement to print
spaces AFTER the spaces, like:
print 'a ', 'b' # prints a b

as opposed to:
print 'a', 'b' # prints a b

Don't do things like string interpolation when they're not needed. It just
makes the code less clear.

- Kef
 
B

Bjorn Pettersen

(e-mail address removed) (KefX) wrote in

This is just overkill.

Actually, it's just plain wrong (sorry). Something similar on the print of
the address would be correct (below)...
No "string interpolation" is needed here, just
the programmer inserted extra spaces and didn't expect the print
statement to print spaces AFTER the spaces, like:
print 'a ', 'b' # prints a b

as opposed to:
print 'a', 'b' # prints a b

Don't do things like string interpolation when they're not needed. It
just makes the code less clear.

I disagree. I prefer more control than print over anything that escapes to
the user... in addition I think

print 'Address: %s, %s, %s, %s' % (sadd, city, state, zip)

is clearer than the comma saturated

print "Address:",saddy,",",city,",",state,",",zip

but that might just be me ;-)

-- bjorn
 
R

Ron Adam

I tried this:
-----------------------------------------------
print "Name: ", fname, mname, lname
print "Address: ",saddy, ",",city, ",",state, ",",zip
-----------------------------------------------

But I get extra spaces after each value:
"Name: Firstname , Middlename , Lastname"
"Address: Street Address , City , State , Zip"

When it should look like this:
"Name: Firstname, Middlename, Lastname"
"Address: Street Address, City, State, Zip"


Hi, I'm new at Python also. This is the what I've found.


saddy='Home'
city='Chair'
state='Happy'
zip='Computer'

# space inserted
print "Address:",saddy,",",city,",",state,",",zip

# no spaces,+ can't be used at beginning or end of line
print "Address:"+saddy+","+city+","+state+","+zip

# space inserted between two prints
print "Address:"+saddy+","+city+",",
print state+","+zip

# same as above
print "Address:%s,%s," % (saddy,city),
print "%s,%s" % (state,zip)

# does not append '\n' character to end
import sys
sys.stdout.write("Address:%s,%s," % (saddy,city))
sys.stdout.write("%s,%s" % (state,zip))

"""
Results in the folling outputs:

Address: Home , Chair , Happy , Computer
Address:Home,Chair,Happy,Computer
Address:Home,Chair, Happy,Computer
Address:Home,Chair, Happy,Computer
Address:Home,Chair,Happy,Computer
"""

The last one give you the most control.
 
K

Kyle E

I finished it. Thanks guys, i'll put a copy below so you can run it and
critique it. I anyone would I would appreciate it!

----------------------------------------------------------

##JJ
from time import sleep
import string

##INTRODUCTION:
print "Personal Information Program"
print "----------------------------"
print
print "Please input the requested information... "
sleep(1)

##INPUT NAME:
print "Name: "
sleep(1)
print "What is your first name?"
fname = raw_input ("> ")
print "What is your middle name?"
mname = raw_input ("> ")
print "What is your last name?"
lname = raw_input ("> ")

##INPUT ADDRESS:
print "Location:"
sleep(1)
print "What is your street address?"
saddy = raw_input ("> ")
print "What city do you live in?"
city = raw_input ("> ")
print "What state do you live in?"
state = raw_input ("> ")
print "What is your zipcode?"
zip = raw_input ("> ")
print "Do you have a P.O. Box?"
poan = raw_input ("> ")
if string.lower(poan) == "yes":
print "What is your P.O. Box number?"
pobox = input ("> ")

##TELEPHONE NUMBERS:
print "Telephone Numbers:"
sleep(1)
print "What is your home telephone number?"
print "Ex. (XXX) XXX-XXXX"
hphn = raw_input("> ")
print "Do you have another phone number?"
phnan = raw_input("> ")
if string.lower(phnan) == "yes":
print "Type of phone..."
print "Ex. Cell, Work, Ect."
typhn = raw_input("> ")
print "What is your", typhn, "number?"
typhnn = raw_input("> ")

##AGE:
print "Age:"
sleep(1)
print "How many years old are you?"
age = raw_input("> ")
print "What is your birthdate?"
print "MM/DD/YYYY"
bdate = raw_input("> ")
sleep(2)

##PRINTING LIST
print
print
print "Results:"
print "Name:", fname, mname, lname
print "Address: %s, %s, %s, %s" %(saddy, city, state, zip)
if string.lower(poan) == "yes":
print "P.O. Box:", pobox
if string.lower(phnan) == "yes":
print "Home Number:", hphn
print typhn, "Number:", typhnn
else:
print "Home Number:",hphn
print "Age:", age
print "Birthdate:", bdate

##SDG
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top