0

Sometimes when I run this in IDLE, the shell will only show

 >>

However, when I close the window and kill the program, it'll appear as normal for a split second before closing. Most of the time it will work though.

userrandomnums
  • 255
  • 1
  • 2
  • 13
  • Please make sure that the indentation here is the same as in your program; it is off after `def play_again():`, for example. – Matthew Adams Nov 11 '12 at 19:51
  • #2, you should post things like this in code review: http://codereview.stackexchange.com/. @GamesBrainiac, it's probably homework but it's specific enough to pass. – unddoch Nov 11 '12 at 20:01
  • Thanks. And yes, it is homework, but it's already completed. I just wanted to learn how I could improve it and use the suggestions here in the future. – userrandomnums Nov 11 '12 at 23:08

1 Answers1

0

For your problem in IDLE, I'd try flushing the stdout regularly to see if it helps at all.

Some comments about your code follow:

  • About the use of global variables, for most cases, you can simply substitute their use by function arguments. When you (think you) need to modify a global variable, generally you're choosing the wrong functions to write. As an example, there's absolutely no need to set the guesses variable inside generate(), you can just do it in play().

  • instead of allowed = ('0','1','2','3','4','5','6','7','8','9','0','+','*'), consider import string ; allowed= list(string.digits+'+'+'*'). Do a dir(string) in the interpreter for more useful variables there.

  • there's no need to evaluate equality of a boolean. while (numguesses < maxguesses) and flag==True: should be while (numguesses < maxguesses) and flag:

  • play_again() is unnecessarily recursive.

  • s1 = secret[0] ; s2 = secret[1] could be s1,s2=secret[:2]

  • for i in range(max): ; num = num + str(randint(0,9)) could be rewritten as "".join([str(randint(0,9)) for i in range(max)]). Some people will argue the first is more readable, it's up for you to decide.

Finally this:

if (i%2 == 0): 
    evaluated = evaluated + s1
else:
    evaluated = evaluated + s2

could be replaced simply by evaluated= (evaluated+s1) if not(i%2) else (evaluated + s2). I think you don't actually need the parenthesis, but they add readability, IMHO

Obviously there's a lot more that could be said about the general structure of the code, I tried to focus on python language features you may not know at this point

Community
  • 1
  • 1
loopbackbee
  • 21,962
  • 10
  • 62
  • 97