Python Flash Card Program

I am new to programming and I am trying to teach myself Python. I started writing a small flash drive script. My idea is that the user has a problem and is asked to send a response. If the answer is incorrect, the user is prompted to try again, so before entering the correct answer. So far, this works as planned, but I don’t know how to get it to start again after entering the correct answer. I suppose I also need to enable some tools that allow the user to exit the game, and I also do not know how to do this if you want to give up an offer that I will be grateful. :-)

PS - If my code is lame, I am open to suggestions. But I'm basically looking for the answer to my original question (s). This is my first foray into coding, so I'm sure time will improve when writing more elegant code.

Thanks!

def add(): from random import randint a = randint(1, 5) b = randint(1, 5) c = a + b print a, '+', b print 'What\ the sum?' d = input() while d != c: print 'Wrong, try again!' d = input() else: print "Correct!" 
+4
source share
2 answers

First, use raw_input instead of input , and then convert it to an integer. To answer your question, ask the user to enter this:

 play_again = raw_input('Play again? (y/n)') if play_again.lower().startswith('y'): add() return 

input is unsafe because it is evaluated as native code, and therefore a qualified person can crack the system. raw_input treats the input as a string, and you can call int() on the result.

This only applies to Python 2. For Python 3, input fine - it replaced Python 2 with raw_input .

In addition, there is recursion in the function - although no one is likely to be persistent enough to overflow the stack, although for other applications iteration will be the best method.

Converting this to an iterative loop is trivial (and also recommended). Put everything separately from the import inside the while True . Then at the end of the loop add this code:

 play_again = raw_input('Play again? (y/n)') if play_again.lower().startswith('y'): continue break 

Of course, you can check 'n' instead of 'y' and get rid of continue by replacing it with a simple break statement.

Note that you can also replace the if with a less readable one:

 if raw_input('Play again? (y/n)').lower().startswith('y'): # or 'n' 

And get rid of the intermediate variable play_again . Although I would not sacrifice readability for compactness.

+3
source

In Python 3 (for input):

I changed your code a bit (added "()" for printing and int so that the comparison works. I didn’t check if you entered not int)

 def add(): from random import randint a = randint(1, 5) b = randint(1, 5) c = a + b print (a, '+', b) d = int(input('What\ the sum?')) while d != c: print ('Wrong, try again!') print (a, '+', b,' ? ') d = int(input()) else: print ("Correct!") while True: add() if input('Continue (O/N) ? ') == 'N': break 

I also printed an addendum in your loop, because many incorrect answers can clear the initial question.

As indicated, the start loop performs your function until you type β€œN”. You can add the correct answer to the add loop if the user wants to exit.

0
source

All Articles