python - Program stuck in while loop not printing -


import random  def usertype():     randletter = random.choice('qwer')     userinput = raw_input('press '+str(randletter))     if userinput == randletter:         return 'correct'     else:         return 'incorrect'  def usertypetest(x,y,result):     while x <= 9:         result = usertype()     if result == 'correct':         x = x+1         y = y+5     else:         x = x+1         y = y-2     return str(y)+'is score'  print usertypetest(0,0,usertype) 

here code. want ask user press button, randomly chosen set (q, w, e, r), print either correct or incorrect, depending on button press. want happen 10 times. after ten tries print score: 5 each 'correct' , -2 'incorrect'. instead receive this.

press e(e) press e(e) press w(e) press q(e) press q(e) press q(e) press r(e) press e(e) press w(e) press q(e) press e(e) press e(e) press e(e) press e(e) press q(e) press w(e) press r(e) press w(e) press r(e) press w(e) press r(e) press r(e) 

regardless of enter, returns neither 'correct', nor 'incorrect'. continues on past 10 , not show score. there problem not seeing.

my input in brackets.

for clarification, want:

press q(q) correct press e(q) incorrect press w(w) correct press q(q) correct press e(eq) incorrect press e(e) correct press q(q) correct press q(r) incorrect press w(w) correct press r(r) correct 29 score 

in python indentation very important.

in code, x while loop never changed if block on same indent level while loop. looped instruction result = usertype()

while x <= 9:     result = usertype() if result == 'correct':     x = x+1     y = y+5 

two additional critiques:

you incrementing x in 2 places, when needs done once.

while x <= 9:     result = usertype()     if result == 'correct':         y = y+5     else:         y = y-2     x += 1 

also, since looping fixed number of times, why not ignore incrementing x , use loop, so:

for x in range(10):     result = usertype()     if result == 'correct':         y = y+5     else:         y = y-2 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -