473,491 Members | 2,221 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Simple Problems: Caps and Commas

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"
Jul 18 '05 #1
6 1682
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
Jul 18 '05 #2
"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
Jul 18 '05 #3
># 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
Jul 18 '05 #4
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
Jul 18 '05 #5
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.
Jul 18 '05 #6
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
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
4876
by: Richard Grove | last post by:
Here's one for you. I import a CSV file which is then read using fgetcsv(); My problem is that if the fields in the CSV file have commas in them, the whole thing bugs out. I am wondering of it is...
3
5064
by: Wade G. Pemberton | last post by:
Help!: I use javascript to format repetitive data from a web page input FORM into a long string , and save it as lines of comma delimited data in a text file on a Unix server. The text...
18
4895
by: Robert | last post by:
Hi! I was wondering if the was any way to determine the state of the caps lock key, on or off. Of course I can capture the key events and see whether the caps lock is pressed, but that does not...
3
2161
by: Iain Miller | last post by:
Can anybody help me with some Access 2000 code? I don't do a lot of coding in Access & so every time I come back to do something I pretty much have to relearn the syntax from scratch so this is...
2
1683
by: Dave | last post by:
I build a very simple program that I put at the end of this post. This program is just supposed to put a button on the screen and exit the program when I press the escape button. But pressing the...
8
3771
by: Cathie | last post by:
Hi guys, I want to do a Server.Transfer to get to a second page, so that I may retrieve variables I have set in the first page. I'm doing that with the usual Server.Transfer("pagename", true). ...
1
15560
by: charlies224 | last post by:
Hi, I am writting a software that requires me to make sure the Num Lock is always on and Caps Lock is always off. First, I know how to detect if Num Lock or Caps Lock is on or off (if...
4
2477
by: striker | last post by:
I have a comma delimited text file that has multiple instances of multiple commas. Each file will contain approximatley 300 lines. For example: one, two, three,,,,four,five,,,,six one, two,...
4
1848
by: ollie.mitch | last post by:
Hi, I need two ereg expressions in my PHP code. One of them needs to check that a string only contains letters, and the other needs to check that the string only contains letters and commas...
0
7115
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7190
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6858
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7360
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4881
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4578
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3086
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
280
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.