• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Simple Python stuff

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

whiterabbit391

Registered
Joined
Aug 2, 2014
I know very little python.

I can make it print and choose my lottery numbers, that is the extent of my programming knowledge.

But I did discover the best way yet to study for college.

I wrote a simple python program to run in the terminal.
It asks a question through raw_input, and compares the input with the answer....like so

Code:
o= raw_input ('what is the syndrome caused by a trisomic defect on chromosone 21 called?:')

if o == 'down syndrome'
	print ('that is correct')
else: 
	print ('sorry that is incorrect, the correct answer is down syndrome')

so on and so forth.

But I have some ideas for different things.

First, I want to know how to incorporate simple loops.
Such as if the input is wrong, it restates the input question, and keeps doing so until you get it right, or gives you three chances, then prints the answer.

Second, i have found that I also like to print study information between the questions,

But it just prints it all at once as a block,
How do I get it to pause before printing,

Or make it look like it's typing, like in the 90's movies where they would type the place and date on the bottom of the screen with a blinking green cursor.......
Even though I know I can't make mine green, it's a stupid light blue, I
 
One of the things that we started looking at was Python for automation of some network tasks. I hadn't personally and TBH, I hate scripting/programming. That's my downfall I suppose.
That being said, this post is of no value as I know nothing about it. :)
 
You could use a while loop for the input until you're happy with the input, such as:

Code:
isvalid = 0

while isvalid == 0:

[INDENT]o= raw_input ('what is the syndrome caused by a trisomic defect on chromosone 21 called?:')

if o == 'down syndrome':
	print ('that is correct')
        isvalid = 1

else: 
	print ('sorry that is incorrect, the correct answer is down syndrome')[/INDENT]

As for pausing whilst outputting, you could get the program to ask for another input from you every so often or you can use the sleep method: http://www.tutorialspoint.com/python/time_sleep.htm
 
You could use a while loop for the input until you're happy with the input, such as:

Code:
isvalid = 0

while isvalid == 0:

[INDENT]o= raw_input ('what is the syndrome caused by a trisomic defect on chromosone 21 called?:')

if o == 'down syndrome':
	print ('that is correct')
        isvalid = 1

else: 
	print ('sorry that is incorrect, the correct answer is down syndrome')[/INDENT]

As for pausing whilst outputting, you could get the program to ask for another input from you every so often or you can use the sleep method: http://www.tutorialspoint.com/python/time_sleep.htm

That code will do what he wants, but it gives the answer after each iteration of the loop.. kind of defeats the purpose of looping. It might be more practical to implement an incrementing counter that gives the user 3 tries, and after the third it tells them the answer and breaks the loop.

Also, OP, since this is a learning exercise for you (in more ways than one, it seems), you could implement reading your questions/answers from a text file. It would decrease the lines of code by a huge margin, and give you the ability to edit your Q&As on the fly (which is useful for this flash-card type of program) without having to edit your code each time.
 
Last edited:
Awesome I never heard of the isvalid term before,(not suprising)
that should be of great help, I'll try it as soon as I get a chance!
 
That's just the name I gave the variable, with Python you don't need to define the type beforehand. You could rename it anything you want, usually to help you remember its purpose.
 
Hey man, did you manage to get this sorted? I've got a quick solution I'd be happy to send you and talk you through :)
It basically uses two different states, one being the correct answered and gives the key information you wanted, and the incorrect answer given tells you to try the answer again.
 
Back