Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 23rd, 2008, 11:11 AM   #21
Chuckiferd
Programmer
 
Join Date: Nov 2007
Posts: 61
Rep Power: 1 Chuckiferd is on a distinguished road
Re: My pyguessinggame

I have debugged this far

#guessgame********** (1/20/08)
from random import randint
def start():
   answer=randint(1,101) #the answer
   guessnum=1 #number of failed user guesses
   guess=raw_imput('''I am thinking of a number that is in between 1 and 100''')#the current guess of the user
   while guess != answer:
      guessnum=guessnum+1
      if guess > 100:
         print '''that guess is way too high, the answer has to be 100 or lower''' 
         guess=raw_imput ('''guess again please, within the limit this time''')
      elif guess == 0:
         print 'actually zero is not within the 1-100 range'
         guess=raw_imput('guess again please')
      elif guess < 0:
         print 'are you testing me?'
         guess=raw_imput('guess again, in the positives please')
      elif guess > 0 and guess < 100 and guess < answer:
         print 'that guess is below the answer'
         guess=raw_imput('try guessing again')
      elif guess >0 and guess < 100 and guess > answer:
         print 'guess lower, your answer is to high'
         guess=raw_imput('try once more')
if guess == answer:
  File "<stdin>", line 22
    if guess == answer:

Syntax Error: invalid syntax

The marker points to the 'F' in 'if', I have experimented over and over with the = sign == sign and all that, I even wrote a test program, look at this.

#this is a test program
ghost=1
answer=1
if ghost==answer:
   print 'boo'
works

Wheras this doesn't
#this is a test program
try=1
  File "<stdin", line 2
try=1

SyntaxError: invalid syntax
answer=1
if try==answer:
File "<stdin>", line 1
   if try==answer:

Syntax Error: invalid syntax
   print 'boo'
  File "<stdin>", line 1
    print 'boo'

IndentationError: unexpected indent
__________________
There are 10 kinds of people in this world, those who can read binary and those who can't.
Chuckiferd is offline   Reply With Quote
Old Jan 23rd, 2008, 12:26 PM   #22
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,722
Rep Power: 5 Sane is on a distinguished road
Re: My pyguessinggame

Not being allowed to assign 1 to "try" is actually a fluke error you stumbled upon. "Try" is a Python keyword related to throwing exceptions. You are not allowed to use Python keywords as variable names.

You have a syntax error in your first piece of code most likely because of something that proceeds that line. I would have to see the rest of the code.

What I said about the while loop, was not the same thing you said. You said if there's a condition within the while loop that matches the end condition of the loop, the latter condition can never be satisfied. But the crucial piece of information is that the while loop test is only performed at the very end/top of the loop's iteration. Not intermediately.

More specifically...

i = 0
while i <= 10:
    i += 1
    print i

Is equivilent to:

i = 0
while 1:
    if not i <= 10: break
    i += 1
    print i

But either way, this is irrelevant. I noticed you're on the right path with how you tried to move guess == answer outside of the while loop.
Sane is offline   Reply With Quote
Old Jan 23rd, 2008, 1:24 PM   #23
Chuckiferd
Programmer
 
Join Date: Nov 2007
Posts: 61
Rep Power: 1 Chuckiferd is on a distinguished road
Re: My pyguessinggame

here ye go

#guessgame********** (1/20/08)
from random import randint
def start():
   answer=randint(1,101) #the answer
   guessnum=1 #number of failed user guesses
   guess=raw_imput('''I am thinking of a number that is in between 1 and 100''')#the current guess of the user
   while guess != answer:
      guessnum=guessnum+1
      if guess > 100:
         print '''that guess is way too high, the answer has to be 100 or lower''' 
         guess=raw_imput ('''guess again please, within the limit this time''')
      elif guess == 0:
         print 'actually zero is not within the 1-100 range'
         guess=raw_imput('guess again please')
      elif guess < 0:
         print 'are you testing me?'
         guess=raw_imput('guess again, in the positives please')
      elif guess > 0 and guess < 100 and guess < answer:
         print 'that guess is below the answer'
         guess=raw_imput('try guessing again')
      elif guess >0 and guess < 100 and guess > answer:
         print 'guess lower, your answer is to high'
         guess=raw_imput('try once more')
if guess == answer:   #!#!#!#!#!#I debugged to here
 ^
SYNTAX ERROR!!! 
  print 'congratualtions, that took', guessnum, 'tries'
if __name__ == '__main__':
   start():
   print
   raw_input('press return')
else:
   print 'Module guessgame imported'
   print 'to run, type:guessgame.start()'
   print 'to refresh, type:reload (guessgame)'
#end game
__________________
There are 10 kinds of people in this world, those who can read binary and those who can't.

Last edited by Chuckiferd; Jan 23rd, 2008 at 1:27 PM. Reason: no BBcode endcode
Chuckiferd is offline   Reply With Quote
Old Jan 23rd, 2008, 1:44 PM   #24
Chuckiferd
Programmer
 
Join Date: Nov 2007
Posts: 61
Rep Power: 1 Chuckiferd is on a distinguished road
Re: My pyguessinggame

I tried forgoing it all and using a else command. But guess what, the last part of the program that gives instructions on how to opperate it if you arn't in main mode has an 'if' statement that doesn't work now, and I can't replace that one.
__________________
There are 10 kinds of people in this world, those who can read binary and those who can't.
Chuckiferd is offline   Reply With Quote
Old Jan 23rd, 2008, 3:34 PM   #25
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,722
Rep Power: 5 Sane is on a distinguished road
Re: My pyguessinggame

The indentation of the print statement right after is wrong.

And then you can't use the variable 'guess' outside the scope of the function. You can use it outside of the scope of the while loop however.

#guessgame********** (1/20/08)
from random import randint
def start():
   answer=randint(1,101) #the answer
   guessnum=1 #number of failed user guesses
   guess=raw_imput('''I am thinking of a number that is in between 1 and 100''')#the current guess of the user
   while guess != answer:
      guessnum=guessnum+1
      if guess > 100:
         print '''that guess is way too high, the answer has to be 100 or lower''' 
         guess=raw_imput ('''guess again please, within the limit this time''')
      elif guess == 0:
         print 'actually zero is not within the 1-100 range'
         guess=raw_imput('guess again please')
      elif guess < 0:
         print 'are you testing me?'
         guess=raw_imput('guess again, in the positives please')
      elif guess > 0 and guess < 100 and guess < answer:
         print 'that guess is below the answer'
         guess=raw_imput('try guessing again')
      elif guess >0 and guess < 100 and guess > answer:
         print 'guess lower, your answer is to high'
         guess=raw_imput('try once more')
   if guess == answer:
      print 'congratualtions, that took', guessnum, 'tries'

Also, you still have a colon on your start():.
Sane is offline   Reply With Quote
Old Jan 23rd, 2008, 5:00 PM   #26
Chuckiferd
Programmer
 
Join Date: Nov 2007
Posts: 61
Rep Power: 1 Chuckiferd is on a distinguished road
Re: My pyguessinggame

great, no more bugs..... now how do I run it? obviously the instructions given by several tutorials "$ python name of program" doesn't work on windows.

Here is the finished product, completely debugged,
#guessgame********** (1/20/08)
from random import randint
def start():
   answer=randint(1,101) #the answer
   guessnum=0 #number of user guesses
   guess=raw_imput('''I am thinking of a number that is in between 1 and 100''')#the current guess of the user
   while guess != answer:
      guessnum=guessnum+1
      if guess > 100:
         print '''that guess is way too high, the answer has to be 100 or lower''' 
         guess=raw_imput ('''guess again please, within the limit this time''')
      elif guess == 0:
         print 'actually zero is not within the 1-100 range'
         guess=raw_imput('guess again please')
      elif guess < 0:
         print 'are you testing me?'
         guess=raw_imput('guess again, in the positives please')
      elif guess > 0 and guess < 100 and guess < answer:
         print 'that guess is below the answer'
         guess=raw_imput('try guessing again')
      elif guess >0 and guess < 100 and guess > answer:
         print 'guess lower, your answer is to high'
         guess=raw_imput('try once more')
   if guess == answer:
      print 'congratualtions, that took', guessnum, 'tries'
   if __name__ == '__main__':
      start()
      print
      raw_input('press return')
   else:
      print 'Module guessgame imported'
      print 'to run, type:guessgame.start()'
      print 'to refresh, type:reload (guessgame)'
#end game
raw_imput('press enter to quit')
__________________
There are 10 kinds of people in this world, those who can read binary and those who can't.
Chuckiferd is offline   Reply With Quote
Old Jan 23rd, 2008, 5:03 PM   #27
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 777
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Re: My pyguessinggame

If you have installed Python, you should be able to run Python files (ending in ".py") just by opening (double-clicking) them.
titaniumdecoy is offline   Reply With Quote
Old Jan 23rd, 2008, 5:31 PM   #28
Chuckiferd
Programmer
 
Join Date: Nov 2007
Posts: 61
Rep Power: 1 Chuckiferd is on a distinguished road
Re: My pyguessinggame

(shakes his head saddly)

The window appears and disapears in less than a second, I see that program never runs either, it is all a blank page

EDIT: I got a screen shot of an error that appears before it terminates, If it is screwed up its not my fault its photobuckets.

PS. I will cut the rest out next time
__________________
There are 10 kinds of people in this world, those who can read binary and those who can't.

Last edited by Chuckiferd; Jan 23rd, 2008 at 5:44 PM.
Chuckiferd is offline   Reply With Quote
Old Jan 23rd, 2008, 5:33 PM   #29
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,722
Rep Power: 5 Sane is on a distinguished road
Re: My pyguessinggame

By the way, for clarification, "bugs" can be logical or syntactical. So it's not entirely "debugged" yet, because you still have "raw_imput" where it should be "raw_input".

You also have:

   if __name__ == '__main__':
      start()
      print
      raw_input('press return')
   else:
      print 'Module guessgame imported'
      print 'to run, type:guessgame.start()'
      print 'to refresh, type:reload (guessgame)'

On the wrong indentation level. Do you understand what it means for a block to be indented? Indentation specifies to which block a line of code belongs. By indenting all that, you're saying it belongs to the function, which it doesn't. It belongs to the main flow of the program on the first indentation level.

Edit: That's the reason it just pops up and exits, because there are still run-time errors. You should be able to run the program from IDLE (Python GUI) by pressing F5 when you have the script opened up for editing.
Sane is offline   Reply With Quote
Old Jan 23rd, 2008, 5:45 PM   #30
Chuckiferd
Programmer
 
Join Date: Nov 2007
Posts: 61
Rep Power: 1 Chuckiferd is on a distinguished road
Re: My pyguessinggame

That is where I originaly had the indentation, The command line disagreed with me untill I changed it to that
__________________
There are 10 kinds of people in this world, those who can read binary and those who can't.
Chuckiferd is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:40 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC