Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #55052

Re: Python Unit Tests

From Terry Reedy <tjreedy@udel.edu>
Subject Re: Python Unit Tests
Date 2013-09-30 02:08 -0400
References <bb6482de-ce4e-4dd2-845c-f71008123c03@googlegroups.com> <mailman.431.1380394053.18130.python-list@python.org> <0dfb1b91-766c-47d5-9708-84b88838d247@googlegroups.com> <5248e816$0$2865$c3e8da3$76491128@news.astraweb.com> <d239fcf5-5730-4017-969d-bd6362a521d1@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.474.1380521312.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 9/30/2013 12:19 AM, melwin9@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Python Unit Tests melwin9@gmail.com - 2013-09-27 21:52 -0700
  Re: Python Unit Tests Dave Angel <davea@davea.name> - 2013-09-28 06:11 +0000
  Re: Python Unit Tests Terry Reedy <tjreedy@udel.edu> - 2013-09-28 14:47 -0400
    Re: Python Unit Tests melwin9@gmail.com - 2013-09-29 18:46 -0700
      Re: Python Unit Tests Steven D'Aprano <steve@pearwood.info> - 2013-09-30 02:55 +0000
        Re: Python Unit Tests melwin9@gmail.com - 2013-09-29 21:19 -0700
          Re: Python Unit Tests Terry Reedy <tjreedy@udel.edu> - 2013-09-30 02:08 -0400
  Re: Python Unit Tests melwin9@gmail.com - 2013-09-30 12:54 -0700
    Re: Python Unit Tests MRAB <python@mrabarnett.plus.com> - 2013-09-30 21:08 +0100
    Re: Python Unit Tests Dave Angel <davea@davea.name> - 2013-09-30 20:20 +0000
    Re: Python Unit Tests Terry Reedy <tjreedy@udel.edu> - 2013-09-30 20:06 -0400

csiph-web