0

Im trying to write a program that assigns an empty str to the variable myvar. then in a while loop the program asks for the user to type text, anything they want and it will print to the screen until the user enters the text quit which will end the loop and stop the program.

this is what I have

myvar = str("")
    while myvar != "quit":
    user_input = raw_input()
    user_input = myvar
print myvar

Thanks.

2
  • I'm afraid you've got some syntax errors there; once you get the indentation right, you can just change line 4 from user_input = myvar to myvar = user_input. Also, this seems like a homework question, which really isn't what StackOverflow is for at all. Commented Jan 18, 2014 at 1:10
  • Its from a practice problem set where I make a flowchart for it. but I dont need to write the code, i'm just doing this for the extra practice. Thanks. Commented Jan 18, 2014 at 1:15

2 Answers 2

3

how about

for output in iter(raw_input,"quit"):
    print output
Sign up to request clarification or add additional context in comments.

2 Comments

Gotta love iter with a sentinel. Although, you don't need the lambda ;-) (lambda :foo() is pretty much the same thing as foo)
stupid me ... usually im casting to an int or something :P (or at least providing a prompt)
1

you're pretty close, but the indentation's off and the logic needs a slight tweak:

myvar = ""
while myvar != "quit":
    myvar = raw_input()
    print myvar

note that this will also print "quit". I'll leave that as a final exercise to you to figure out how to cut that out.

7 Comments

Thanks. I see what I was doing wrong. The quit thing is something ill look into. Give me a few and ill post back with an answer.
+1 for giving an answer based on OP's code... that should help him learn and get better at coding
are you being sarcastic or serious?
serious ... entirely ... im rarely sarcastic on SO, and genuinely like to see people learn from the answers
for the program to not print "quit" dont you use the sysexit()
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.