Newbie Loop Question (Python)

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

Newbie Loop Question (Python)

Post by Spectre557 on Mon Jul 06, 2009 4:19 pm
([msg=26333]see Newbie Loop Question (Python)[/msg])

Hey,

After a long break I'm having another foray into the world of programming in Python, and I'm really stuck with something. Loops. :? I think.

I think I need to use a loop for the desired effect, but I'm not sure. Basically what I want to do is this:
- Ask the user a question, expecting a yes or no input
- If they say yes, do one thing
- If they say no, do another thing
- If they input anything other than yes or no, repeat the question.

I want to loop in the sense that I want it to keep repeating the question until a sufficient answer is given. But having looked at several tutorials and online sources, I can't get my head around how to use a loop for anything more than a very basic "keep doing x until y is z", typically like this:
Code: Select all
x = 1
while x < 10:
   print "blah"
   x = x + 1
print "blahblahblah"

The problem is I don't want it to just keep doing something, I want it to actually return to an earlier point in the code, and continue from there, not do the same thing over and over. Or am I looking at this the wrong way?

Someone please enlighten me!
Current obsession: Minecraft
User avatar
Spectre557
Poster
Poster
 
Posts: 215
Joined: Wed Apr 29, 2009 4:04 am
Blog: View Blog (0)


Re: Newbie Loop Question (Python)

Post by aNewHobby4me on Mon Jul 06, 2009 5:30 pm
([msg=26344]see Re: Newbie Loop Question (Python)[/msg])

Try using this as an example.

Its similar to what you are describing, maybe enough to help.

[edited link]
"To understand recursion you must first understand recursion."
aNewHobby4me
Poster
Poster
 
Posts: 185
Joined: Thu Jan 08, 2009 5:44 pm
Blog: View Blog (0)


Re: Newbie Loop Question (Python)

Post by Defience on Mon Jul 06, 2009 7:55 pm
([msg=26357]see Re: Newbie Loop Question (Python)[/msg])

It's kind of like the question you had posted before about the "if/elif/else" usage. You can use a simple 'While' loop like this:

Code: Select all
while True:
    response = raw_input("Are you an elite hacker?  ")
    if response.lower() == "yes":
         print "Can you hack a myspace for me then?!"
         break
    elif response.lower() == "no":
        print "Poser! Do you know anyone that is?"
        break
    else:
        print "Please enter a yes or no or I will keep asking the same question!"
User avatar
Defience
Addict
Addict
 
Posts: 1281
Joined: Thu Jun 12, 2008 3:16 pm
Blog: View Blog (0)


Re: Newbie Loop Question (Python)

Post by Spectre557 on Mon Jul 06, 2009 8:57 pm
([msg=26360]see Re: Newbie Loop Question (Python)[/msg])

Defience wrote:It's kind of like the question you had posted before about the "if/elif/else" usage. You can use a simple 'While' loop like this:

Code: Select all
while True:
    response = raw_input("Are you an elite hacker?  ")
    if response.lower() == "yes":
         print "Can you hack a myspace for me then?!"
         break
    elif response.lower() == "no":
        print "Poser! Do you know anyone that is?"
        break
    else:
        print "Please enter a yes or no or I will keep asking the same question!"

Ahhh, that would appear to be exactly what I was looking for. Thanks mate! :)
Current obsession: Minecraft
User avatar
Spectre557
Poster
Poster
 
Posts: 215
Joined: Wed Apr 29, 2009 4:04 am
Blog: View Blog (0)


Re: Newbie Loop Question (Python)

Post by Defience on Mon Jul 06, 2009 9:11 pm
([msg=26362]see Re: Newbie Loop Question (Python)[/msg])

No problem. Glad I could help 8-)
User avatar
Defience
Addict
Addict
 
Posts: 1281
Joined: Thu Jun 12, 2008 3:16 pm
Blog: View Blog (0)


Re: Newbie Loop Question (Python)

Post by Spectre557 on Thu Jul 09, 2009 12:05 pm
([msg=26545]see Re: Newbie Loop Question (Python)[/msg])

Hmm, I've had a go with this, and I think I understand it pretty well now, but I've encountered another problem with the while loop.

What I want to do is print a series of lines of text on screen over and over until input is given. My only problem is, how would I maintain a raw_input throughout the loop?

Or in other words, I want a loop with raw_input as a break.

Does that make any sense? :|
Current obsession: Minecraft
User avatar
Spectre557
Poster
Poster
 
Posts: 215
Joined: Wed Apr 29, 2009 4:04 am
Blog: View Blog (0)


Re: Newbie Loop Question (Python)

Post by insomaniacal on Thu Jul 09, 2009 12:21 pm
([msg=26548]see Re: Newbie Loop Question (Python)[/msg])

I don't think you'll be able to use raw_input for that, because as soon as the script reaches "raw_input" in the code, it will stop executing the loop and wait for you to input something.
It's not who votes that counts, it's who counts the votes
insomaniacal.blog.com
User avatar
insomaniacal
Addict
Addict
 
Posts: 1210
Joined: Sun May 24, 2009 10:21 am
Blog: View Blog (0)


Re: Newbie Loop Question (Python)

Post by Spectre557 on Thu Jul 09, 2009 1:07 pm
([msg=26551]see Re: Newbie Loop Question (Python)[/msg])

insomaniacal wrote:I don't think you'll be able to use raw_input for that, because as soon as the script reaches "raw_input" in the code, it will stop executing the loop and wait for you to input something.

That makes sense...

So do you know what I could use instead?
Current obsession: Minecraft
User avatar
Spectre557
Poster
Poster
 
Posts: 215
Joined: Wed Apr 29, 2009 4:04 am
Blog: View Blog (0)


Re: Newbie Loop Question (Python)

Post by insomaniacal on Thu Jul 09, 2009 1:22 pm
([msg=26552]see Re: Newbie Loop Question (Python)[/msg])

I'm not an expert as far as programming goes, but I don't think you could do this in Python.

Maybe if you somehow wrote the script so that any button you press counts as a keyboard interrupt, but I have no clue if that is possible to do, and if it is, I've no idea on how to do that.
It's not who votes that counts, it's who counts the votes
insomaniacal.blog.com
User avatar
insomaniacal
Addict
Addict
 
Posts: 1210
Joined: Sun May 24, 2009 10:21 am
Blog: View Blog (0)


Re: Newbie Loop Question (Python)

Post by Defience on Thu Jul 09, 2009 4:30 pm
([msg=26562]see Re: Newbie Loop Question (Python)[/msg])

Ctrl -c will get the process to stop running, but I'm not sure I'm following what it is you're looking for. Did you want output something like this example:

>>>Enter yes or no:yes
Good choice
>>>Enter yes or no:no
Bad choice
>>>Enter yes or no:maybe
>>>Enter yes or no:
>>>Enter yes or no:
>>>Enter yes or no:
>>>Enter yes or no:
>>>Enter yes or no:
>>>Enter yes or no:
>>>Enter yes or no:
>>>Enter yes or no:

so that the question will keep running until a yes or no is entered? If so, I don't see how that can be done because it's not going to accept new input until it stops running. Or am I misunderstanding you?
User avatar
Defience
Addict
Addict
 
Posts: 1281
Joined: Thu Jun 12, 2008 3:16 pm
Blog: View Blog (0)


Next

Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests

cron