Novice Issue

B

Bradley Wright

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
 
C

Chris Angelico

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
 
W

Wolfgang Maier

Bradley Wright said:
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
 
C

Chris Angelico

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
 
M

Mark Lawrence

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 :)
 
B

Bradley Wright

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
 
D

Dave Angel

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

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.
 
B

Bradley Wright

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
 
J

John Gordon

In said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top