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
simple loops
Page 1 of 13 Replies - 1954 Views - Last Post: 22 October 2006 - 06:14 PM
Replies To: simple loops
#2
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........
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"
Metalhead, on 22 Oct, 2006 - 01:05 PM, said:
For example:
What is the capital of France?
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
#3
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!
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!
#4
Re: simple loops
Posted 22 October 2006 - 06:14 PM
Metalhead, 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?
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? ' )
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|