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" 6 1637
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
"Kyle E" <Ky********@hotmail.com> wrote in
news:10***************@news001.transaeris.com: 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?
# 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: ----------------------------------------------- print "Name: ", fname, mname, lname print "Address: ",saddy, ",",city, ",",state, ",",zip -----------------------------------------------
But I get extra spaces after each value:
# the % operator does 'string interpolation' and gives you more control
# over the outputstring
print 'Name: %s, %s, %s' % (fname, mname, lname)
hth,
-- bjorn
># 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 ke**********@aol.comNOSPAM (KefX) wrote in
news:20***************************@mb-m04.aol.com: # 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.
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
On Sun, 26 Oct 2003 23:24:46 -0600, "Kyle E" <Ky********@hotmail.com>
wrote: 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.
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
----------------------------------------------------------
--
Kyle E
JJ SDG
Missouri This discussion thread is closed Replies have been disabled for this discussion. Similar topics
5 posts
views
Thread by Richard Grove |
last post: by
|
3 posts
views
Thread by Wade G. Pemberton |
last post: by
|
18 posts
views
Thread by Robert |
last post: by
|
3 posts
views
Thread by Iain Miller |
last post: by
|
2 posts
views
Thread by Dave |
last post: by
|
8 posts
views
Thread by Cathie |
last post: by
|
1 post
views
Thread by charlies224 |
last post: by
|
4 posts
views
Thread by striker |
last post: by
|
4 posts
views
Thread by ollie.mitch |
last post: by
| | | | | | | | | | |