3 Replies - 2104 Views - Last Post: 22 October 2006 - 06:14 PM Rate Topic: -----

#1 Metalhead  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-October 06

simple loops

Posted 22 October 2006 - 01:05 PM

Hi,
I'm new to Python and OO in general. I am trying to set up a simple loop based around an inputed answer to a question, so that if you input a wrong answer, you are asked the question again, but if correct, the program moves on.

For example:
What is the capital of France?

Correct answer - move on to next question. Wrong answer. Repeat the question

I used to write in basic years ago in which programs had line numbers and would have went something like this. It was easy to direct the program back again to ask the question again.


10 print "what is the capital of France?"
20 get a$ #user inputs answer
30 If A$ = "Paris" then goto 60
40 print "That is the wrong answer!. Try again"
50goto 10
60 The next question........

I'm really finding it hard to get my head round loops without line numbers.
thanks,
t

Is This A Good Question/Topic? 0
  • +

Replies To: simple loops

#2 rockstar_  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 189
  • Joined: 16-October 06

Re: simple loops

Posted 22 October 2006 - 01:33 PM

Quote

10 print "what is the capital of France?"
20 get a$ #user inputs answer
30 If A$ = "Paris" then goto 60
40 print "That is the wrong answer!. Try again"
50goto 10for other progr
60 The next question........

Ah, I see what you're saying. Yeah, the GOTO stuff was cool for easy programs, but when they got big, the diagrams wer HUGE. I've never actually written in it, but I had an older friend who did a lot of his work in it, and I found it an absolute AMAZEMENT he knew what he was doing. I asked him one time how he documented the code for other programmers, he said "We didn't"

View PostMetalhead, on 22 Oct, 2006 - 01:05 PM, said:

For example:
What is the capital of France?

I think what you really want to read up on is a while loop. For instance, you could do something like:
answer = 'Paris'
while answer != userAnswer:
  userAnswer = raw_input( 'What is the capital of France? ' )


You could build on that idea to output "Wrong answer" and things like that, but there's a start.

rockstar
Was This Post Helpful? 0
  • +
  • -

#3 Metalhead  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-October 06

Re: simple loops

Posted 22 October 2006 - 02:08 PM

Brilliant. Thanks for that - it works well! Do you know how I set up the answer for more than one possible answer?
i.e. Paris , paris or PARIS. or for that matter, just make it case insensitive?

Thanks,
tim

p.s. I know what you mean about Gotos. Plans for complex programs got confusing, but the language itself was so simple!
Was This Post Helpful? 0
  • +
  • -

#4 rockstar_  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 189
  • Joined: 16-October 06

Re: simple loops

Posted 22 October 2006 - 06:14 PM

View PostMetalhead, on 22 Oct, 2006 - 02:08 PM, said:

Brilliant. Thanks for that - it works well! Do you know how I set up the answer for more than one possible answer?
i.e. Paris , paris or PARIS. or for that matter, just make it case insensitive?

Well, I would suggest you convert the user's answer to lowercase, and store all the correct answers lowercase as well. That way, someone could input 'PaRiS' or 'paRIS', and it would return the same output. So the above code would have to be modified to look like this:
import string
answer = 'paris'
while answer != string.lower( userAnswer ):
  userAnswer = raw_input( 'What is the capital of France? ' )


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1