Python Unit Tests

melwin9 at gmail.com melwin9 at gmail.com
Mon Sep 30 00:19:26 EDT 2013


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

[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!')
   else:
     print(goodbye + ' The number I was thinking of was ' + number)

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


On Sunday, September 29, 2013 10:55:19 PM UTC-4, Steven D'Aprano wrote:
> On Sun, 29 Sep 2013 18:46:30 -0700, melwin9 wrote:
> 
> 
> 
> > Hi Terry & Dave,
> 
> > 
> 
> > Thanks for the suggestions. I am running Python 2.7 and when I tried the
> 
> > code above it said def main(target) <- invalid syntax. 
> 
> 
> 
> 
> 
> Did you fix the invalid syntax? Until you fix it, you're blocked.
> 
> 
> 
> As a programmer, it is completely normal to occasionally make a typo or 
> 
> other trivial mistake that leads to invalid syntax. It's not worth 
> 
> mentioning. You just fix it and move on.
> 
> 
> 
> It's a bit like being a cook. The recipe says, "add one tablespoon of 
> 
> sugar", and you say "well I tried, but the spoon bangs on the lid of the 
> 
> jar." The solution is to take the lid off first. The recipe doesn't 
> 
> mention this, just like the recipe doesn't say "take the spoon out of the 
> 
> cutlery drawer". You just do it.
> 
> 
> 
> If you don't understand the syntax error you got, firstly try to compare 
> 
> the bad syntax
> 
> 
> 
> def main(target)
> 
>     blah blah blah
> 
> 
> 
> 
> 
> with working functions that don't give syntax errors
> 
> 
> 
> def function(arg):
> 
>     blah blah blah
> 
> 
> 
> 
> 
> Can you spot the difference? Hint: the syntax error will show an arrow ^ 
> 
> pointing at the spot where it notices a problem. Can you fix the problem? 
> 
> If so, great, move on! If not, ask for help, but remember to COPY AND 
> 
> PASTE the entire traceback, starting with the line
> 
> 
> 
> Traceback (most recent call last)
> 
> 
> 
> all the way to the end of the error message.
> 
> 
> 
> 
> 
> 
> 
> -- 
> 
> Steven




More information about the Python-list mailing list