3
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 your score'

print usertypetest(0,0,usertype)

Here is my code. I want it to ask the user to press a button, randomly chosen from the set (Q, W, E, R), then print either correct or incorrect, depending on which button they press. I want this to happen 10 times. After ten tries it will print their score: 5 for each 'Correct' and -2 for 'Incorrect'. Instead I 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 what I enter, it returns neither 'Correct', nor 'Incorrect'. It also continues on past 10 and does not show their score. There is clearly a problem I am not seeing.

My input is in brackets.

For clarification, this is what I 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 is your score
Bretsky
  • 423
  • 8
  • 23

7 Answers7

3

In Python indentation is very important.

In this code, the x for the while loop is never changed as the if block is on the same indent level as the while loop. So the only looped instruction is result = usertype()

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

Two additional critiques:

You are incrementing x in two places, when it only needs to be done once.

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

Also, since you are looping a fixed number of times, why not ignore incrementing x and use a for loop, like so:

for x in range(10):
    result = usertype()
    if result == 'Correct':
        y = y+5
    else:
        y = y-2
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    Despite the reference to 9, the original loop actually iterates 10 times, so the replacement needs `range(10)`. But using `range()` is definitely more sensible. – Jonathan Leffler Jun 27 '14 at 04:40
  • Whooops. [Fencepost error](http://en.wikipedia.org/wiki/Off-by-one_error#Fencepost_error) –  Jun 27 '14 at 04:42
2

You need to put the if result == 'Correct': block under the while x <= 9: loop where you get the user input, so that it gets evaluate every time. And you can add the print(result) to get the correct/incorrect feedback as in your example:

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
        print(result)
    return str(y)+'is your score'
famousgarkin
  • 13,687
  • 5
  • 58
  • 74
1

Your main problem was that you had the if/else block in the wrong scope. You needed it to be under the while block. This makes sure that it checks whether the user entered the correct input each time usertype() is run.

import random

moves = 0
score = 0

def usertype():
    randletter = random.choice('qwer')
    userinput = raw_input('Press '+str(randletter))
    if userinput == randletter:
        return True
    else:
        return False

def usertypetest(moves, score):
    while moves < 10:
        result = usertype()
        moves = moves + 1
        if result:
            score = score + 5
        else:
            score = score - 2
    return str(score) + ' is your score'

print usertypetest(moves, score)
royhowie
  • 11,075
  • 14
  • 50
  • 67
1

Also, you are not printing the value of the variable result. After evaluating result, add the following:

print result

red-goblin
  • 41
  • 3
1

Apart from the indentation problem that other people have identified, the code is not particularly idiomatic Python. The usertypetest() function could be:

def usertypetest(x,y,result):
    for x in range(10):
        if usertype() == 'Correct':
            y = y + 5
        else:
            y = y - 2
    return '%d is your score' % y

There might be better ways to do this, but this is a little simpler and a little more Pythonic.

I'll also observe that I don't see the parentheses around the input that the example claims to see.

If you want to see the verdict on each letter, then you need to save the return from usertype() after all:

def usertypetest(x,y,result):
    for x in range(10):
        result = usertype()
        print result
        if result == 'Correct':
            y = y + 5
        else:
            y = y - 2
    return '%d is your score' % y
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Thanks for showing me how to simplify it. Although the first answerer produced functional code, you're getting +1 for the simplifying. Also I just put the parentheses there to identify the input, they were not in the actual output. – Bretsky Jun 27 '14 at 04:41
0

Just put if block under the while loop. Problem solved.

Try with this code :

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 your score'

print usertypetest(0,0,usertype)
Surinder ツ
  • 1,778
  • 3
  • 16
  • 27
0

There are several problems here.

  1. You need to print 'correct' before usertype returns.
  2. You don't need to put 'result' in usertypetest.
  3. Put if...else inside the loop in usertypetest.
  4. Change your last print.

Here is the correct code.

import random

def usertype():
    randletter = random.choice('qwer')
    userinput = raw_input('Press '+str(randletter))
    if userinput == randletter:
        print 'Correct'
        return 'Correct'
    else:
        print 'Incorrect'
        return 'Incorrect'

def usertypetest(x,y):
    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 your score'

print usertypetest(0,0)