Novice Issue

Wolfgang Maier wolfgang.maier at biologie.uni-freiburg.de
Thu Apr 18 04:58:56 EDT 2013


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






More information about the Python-list mailing list