Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #43805 > unrolled thread

Novice Issue

Started byBradley Wright <bradley.wright.biz@gmail.com>
First post2013-04-17 21:06 -0700
Last post2013-04-18 14:21 +0000
Articles 9 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  Novice Issue Bradley Wright <bradley.wright.biz@gmail.com> - 2013-04-17 21:06 -0700
    Re: Novice Issue Chris Angelico <rosuav@gmail.com> - 2013-04-18 14:14 +1000
    Re: Novice Issue Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2013-04-18 08:58 +0000
    Re: Novice Issue Chris Angelico <rosuav@gmail.com> - 2013-04-18 19:06 +1000
    Re: Novice Issue Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-04-18 11:10 +0100
    Re: Novice Issue Bradley Wright <bradley.wright.biz@gmail.com> - 2013-04-18 05:18 -0700
      Re: Novice Issue Dave Angel <davea@davea.name> - 2013-04-18 08:29 -0400
    Re: Novice Issue Bradley Wright <bradley.wright.biz@gmail.com> - 2013-04-18 05:34 -0700
    Re: Novice Issue John Gordon <gordon@panix.com> - 2013-04-18 14:21 +0000

#43805 — Novice Issue

FromBradley Wright <bradley.wright.biz@gmail.com>
Date2013-04-17 21:06 -0700
SubjectNovice Issue
Message-ID<0fa050c1-3a00-4c17-9fa6-b79a22485c7a@googlegroups.com>
Good Day all, currently writing a script that ask the user for three things;
1.Name
2.Number
3.Description
I've gotten it to do this hurah!

print "Type \"q\" or \"quit\" to quit"
while raw_input != "quit" or "q":
    
    print ""
    name = str(raw_input("Name: "))
    number = str(raw_input("Number: "))
    description = str(raw_input("Description: "))

but here a few things, can anyone help me on figuring out how to at the users whim print out all of the names, numbers and descriptions. this is sort of an information logger.

additionally, minor issue with getting script to stop when q or quit is typed

any help would be greatly appreciated

[toc] | [next] | [standalone]


#43806

FromChris Angelico <rosuav@gmail.com>
Date2013-04-18 14:14 +1000
Message-ID<mailman.754.1366258490.3114.python-list@python.org>
In reply to#43805
On Thu, Apr 18, 2013 at 2:06 PM, Bradley Wright
<bradley.wright.biz@gmail.com> wrote:
> Good Day all, currently writing a script that ask the user for three things;
> 1.Name
> 2.Number
> 3.Description
> I've gotten it to do this hurah!
>
> print "Type \"q\" or \"quit\" to quit"
> while raw_input != "quit" or "q":

You'll want to actually _call_ that function: raw_input()

>
>     print ""
>     name = str(raw_input("Name: "))
>     number = str(raw_input("Number: "))
>     description = str(raw_input("Description: "))

raw_input already returns a string, so the str() is redundant here.

> but here a few things, can anyone help me on figuring out how to at the users whim print out all of the names, numbers and descriptions. this is sort of an information logger.
>
> additionally, minor issue with getting script to stop when q or quit is typed
>
> any help would be greatly appreciated

Once you have all the values, you just need to figure out what you're
trying to do with them. Do you need to retain them till the end of the
loop? If so, consider a list or dictionary. Or do you need to work
with them right there inside the loop? What are you needing to
accomplish?

ChrisA

[toc] | [prev] | [next] | [standalone]


#43811

FromWolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de>
Date2013-04-18 08:58 +0000
Message-ID<mailman.757.1366275551.3114.python-list@python.org>
In reply to#43805
Bradley Wright <bradley.wright.biz <at> gmail.com> writes:

> 
> Good Day all, currently writing a script that ask the user for three things;
> 1.Name
> 2.Number
> 3.Description
> I've gotten it to do this hurah!
> 
> print "Type \"q\" or \"quit\" to quit"
> while raw_input != "quit" or "q":
> 
>     print ""
>     name = str(raw_input("Name: "))
>     number = str(raw_input("Number: "))
>     description = str(raw_input("Description: "))
> 
> but here a few things, can anyone help me on figuring out how to at the
users whim print out all of the names,
> numbers and descriptions. this is sort of an information logger.
> 
> additionally, minor issue with getting script to stop when q or quit is typed

your minor issue here is your "or" test, which is not doing what you think
it does.
You're testing here for either of the following to conditions:
1) raw_input != "quit"
2) "q" (Python can't know that you want raw_input != "q" here!!)
Now any non-empty string in Python tests True, so your while loop never stops.
There are two solutions for that:
the obvious: while not (raw_input == "quit" or raw_input == "q")
or the pythonic way: while raw_input not in ("quit", "q")
The second form definitely is preferable over the first when you have to
test for more than two conditions.
For your other questions see Chris' answers.

Best,
Wolfgang


[toc] | [prev] | [next] | [standalone]


#43812

FromChris Angelico <rosuav@gmail.com>
Date2013-04-18 19:06 +1000
Message-ID<mailman.758.1366275971.3114.python-list@python.org>
In reply to#43805
On Thu, Apr 18, 2013 at 6:58 PM, Wolfgang Maier
<wolfgang.maier@biologie.uni-freiburg.de> wrote:
> There are two solutions for that:
> the obvious: while not (raw_input == "quit" or raw_input == "q")

That has another problem: Once that's changed to raw_input() so it
actually requests input, it will do so twice, compare the first line
against "quit" and the second against "q", and proceed on that basis.
The membership test raw_input() in ("quit","q") is going to be far
better.

ChrisA

[toc] | [prev] | [next] | [standalone]


#43816

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2013-04-18 11:10 +0100
Message-ID<mailman.761.1366279865.3114.python-list@python.org>
In reply to#43805
On 18/04/2013 05:06, Bradley Wright wrote:
> Good Day all, currently writing a script that ask the user for three things;
> 1.Name
> 2.Number
> 3.Description
> I've gotten it to do this hurah!
>
> print "Type \"q\" or \"quit\" to quit"

You've had a couple of answers already so I'll just point out that the 
above line could be.

print 'Type "q" or "quit" to quit'

Looks prettier if nothing else :)

> while raw_input != "quit" or "q":
>
>      print ""
>      name = str(raw_input("Name: "))
>      number = str(raw_input("Number: "))
>      description = str(raw_input("Description: "))
>
> but here a few things, can anyone help me on figuring out how to at the users whim print out all of the names, numbers and descriptions. this is sort of an information logger.
>
> additionally, minor issue with getting script to stop when q or quit is typed
>
> any help would be greatly appreciated
>

-- 
If you're using GoogleCrap™ please read this 
http://wiki.python.org/moin/GoogleGroupsPython.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#43819

FromBradley Wright <bradley.wright.biz@gmail.com>
Date2013-04-18 05:18 -0700
Message-ID<36c94c5e-6ad1-4aba-b18c-61cf14cf102c@googlegroups.com>
In reply to#43805
On Thursday, April 18, 2013 12:06:59 AM UTC-4, Bradley Wright wrote:
> Good Day all, currently writing a script that ask the user for three things;
> 
> 1.Name
> 
> 2.Number
> 
> 3.Description
> 
> I've gotten it to do this hurah!
> 
> 
> 
> print "Type \"q\" or \"quit\" to quit"
> 
> while raw_input != "quit" or "q":
> 
>     
> 
>     print ""
> 
>     name = str(raw_input("Name: "))
> 
>     number = str(raw_input("Number: "))
> 
>     description = str(raw_input("Description: "))
> 
> 
> 
> but here a few things, can anyone help me on figuring out how to at the users whim print out all of the names, numbers and descriptions. this is sort of an information logger.
> 
> 
> 
> additionally, minor issue with getting script to stop when q or quit is typed
> 
> 
> 
> any help would be greatly appreciated

Joining this group has been the smartest decision since birth 

Firstly, thanks Chris - str() [REMOVED]
and yes i would like to retain them until the end of the loop simply for printing or viewing purposes. [I'll DO MORE RESEARCH ON LISTS] 

Secondly, thanks Wolfgang
while raw_input not in ("quit", "q") - genius, i get your point clearly

Thirdly, thanks mark
went through the link you provided, insightful

Cheers

[toc] | [prev] | [next] | [standalone]


#43820

FromDave Angel <davea@davea.name>
Date2013-04-18 08:29 -0400
Message-ID<mailman.764.1366288199.3114.python-list@python.org>
In reply to#43819
On 04/18/2013 08:18 AM, Bradley Wright wrote:
>
>    <SNIP>
>>

> Secondly, thanks Wolfgang
> while raw_input not in ("quit", "q") - genius, i get your point clearly

But you have to combine his point with Chris's, don't forget the parens 
on the call to raw_input.  And if it were I, I'd also put a prompt 
string in the call.

    while raw_input("q to quit") not in ("quit", "q"):

>

If you've got a list, and each time through you want to add some item(s) 
to a list, you can use the following idiom:

result = []     #empty list
for   whatever in something:
     value = another
     result.append(value)

Then when you finish the list, you can examine the whole list.

-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#43821

FromBradley Wright <bradley.wright.biz@gmail.com>
Date2013-04-18 05:34 -0700
Message-ID<cf7b702e-1c56-4aba-a39b-dc03d9934fdf@googlegroups.com>
In reply to#43805
On Thursday, April 18, 2013 12:06:59 AM UTC-4, Bradley Wright wrote:
> Good Day all, currently writing a script that ask the user for three things;
> 
> 1.Name
> 
> 2.Number
> 
> 3.Description
> 
> I've gotten it to do this hurah!
> 
> 
> 
> print "Type \"q\" or \"quit\" to quit"
> 
> while raw_input != "quit" or "q":
> 
>     
> 
>     print ""
> 
>     name = str(raw_input("Name: "))
> 
>     number = str(raw_input("Number: "))
> 
>     description = str(raw_input("Description: "))
> 
> 
> 
> but here a few things, can anyone help me on figuring out how to at the users whim print out all of the names, numbers and descriptions. this is sort of an information logger.
> 
> 
> 
> additionally, minor issue with getting script to stop when q or quit is typed
> 
> 
> 
> any help would be greatly appreciated

Thanks Dave quite helpful as well,
now i have a clear road to go on, with regards the lists portion of my script

[toc] | [prev] | [next] | [standalone]


#43832

FromJohn Gordon <gordon@panix.com>
Date2013-04-18 14:21 +0000
Message-ID<kkovhf$ftp$1@reader1.panix.com>
In reply to#43805
In <0fa050c1-3a00-4c17-9fa6-b79a22485c7a@googlegroups.com> Bradley Wright <bradley.wright.biz@gmail.com> writes:

> while raw_input != "quit" or "q":

Others have pointed out flaws in this statement.  However, even if you
had written the loop the 'correct' way:

    user_input = raw_input()
    while user_input != "quit" or user_input != "q":

There is still a logic bug.  This loop will execute forever, because no
matter what the user enters, it will be unequal to "q" or unequal to "quit".
Use 'and' instead of 'or'.

Of course in this specific situation, as others have suggested, 'in' is
better still.

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web