Python Unit Tests

Terry Reedy tjreedy at udel.edu
Mon Sep 30 02:08:17 EDT 2013


On 9/30/2013 12:19 AM, melwin9 at gmail.com wrote:
> Hi Dave,
>
> Yeah I found the silly mistake lol thanks for that. making progress.
>
>
> Guess a number: 5
> That's too high.
> Guess a number: 4
> That's too high.
> Guess a number: 3
> Traceback (most recent call last):
>    File "guess.py", line 34, in <module>
>      main(random.randint(1, 10))
>    File "guess.py", line 29, in main
>      print(responseCorrect + '! You guessed my number in ' + tries + 'guesses!')
> TypeError: cannot concatenate 'str' and 'int' objects

This is what 'untested' means ;-)

> [code]import random
>
> intro = 'I have chosen a number from 1-10'
> request = 'Guess a number: '
> responseHigh = "That's too high."
> responseLow  = "That's too low."
> responseCorrect = "That's correct!"
> responseWrong = "Wrong, The correct number is "
> guessTaken = "Your number of guesses were "
> goodbye = ' Goodbye and thanks for playing!'
>
> allowed = 5
>
> def getguess(target, allowed):
>     tries = 0
>     while tries < allowed:
>       tries += 1
>       guess = int(input(request))
>       if guess < target:
>         print(responseLow)
>       elif guess > target:
>         print(responseHigh)
>       else:
>         return guess, tries
>
> def main(target):
>     guess, tries = getguess(target, allowed)
>     if guess == target:
>         print(responseCorrect + '! You guessed my number in ' + tries + 'guesses!')

Either change 'tries' to 'str(tries)' or replace the statement with

        print(responseCorrect, 'You guessed my number in', tries, 
'guesses!')

There is no need to create a single string before the print.

>     else:
>       print(goodbye + ' The number I was thinking of was ' + number)

ditto

>
> if __name__ == '__main__':
>     main(random.randint(1, 10)) [/code]

-- 
Terry Jan Reedy




More information about the Python-list mailing list