473,587 Members | 2,324 Online
Bytes | Software Development & Data Engineering Community
+ 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 1693
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(po an) == "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********@hot mail.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(po an)...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**********@ao l.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:",sadd y,",",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********@hot mail.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:",sadd y,",",city,",", state,",",zip

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

# space inserted between two prints
print "Address:"+sadd y+","+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.writ e("Address:%s,% s," % (saddy,city))
sys.stdout.writ e("%s,%s" % (state,zip))

"""
Results in the folling outputs:

Address: Home , Chair , Happy , Computer
Address:Home,Ch air,Happy,Compu ter
Address:Home,Ch air, Happy,Computer
Address:Home,Ch air, Happy,Computer
Address:Home,Ch air,Happy,Compu ter
"""

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(po an) == "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(ph nan) == "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(po an) == "yes":
print "P.O. Box:", pobox
if string.lower(ph nan) == "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
4884
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 possible to replace these commas in the fields without replacing the CSV structure. Any help greatly appreciated. Regards
3
5069
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 file is meant to be downloaded to MS Excel on the users' computers. Several such files will be downloaded and appended into the Excel file. Problem:...
18
4917
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 help. I have seen some example that looks at the characters entered in an input field to determine if the caps lock is on, but I was wondering if...
3
2166
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 taking me hours. As with most occasional programmers its the commas, colons, inverted commas, square brackets etc etc that cause the problem! (Not...
2
1688
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 escape button doesn't do anything if I put the code for the button into the program! infact, pressing escape only works if you take away the little...
8
3780
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). My problems are as follows: 1. On the initial load everything seems to be referencing from the first page. For example, my style sheet I...
1
15623
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 someone is interested, let me know and I will send you the codes). Once we know if the stat of Num Lock/ Caps Lock is not what we desired, we just send...
4
2479
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, three,four,,,,,,,,,,eighteen, and so on. There is one time when multiple commas are allowed. Just prior to the letters ADMNSRC there should be...
4
1865
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 (only one comma at each time). I thought that the code for only containing letters would be: eregi("^()", $keywords);
0
7843
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8340
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7967
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5713
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3840
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3875
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2353
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1185
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.