![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#21 |
|
Programmer
Join Date: Nov 2007
Posts: 61
Rep Power: 1
![]() |
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 syntaxThe 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' 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. |
|
|
|
|
|
#22 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,722
Rep Power: 5
![]() |
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 iIs equivilent to: i = 0
while 1:
if not i <= 10: break
i += 1
print iBut 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. ![]() |
|
|
|
|
|
#23 |
|
Programmer
Join Date: Nov 2007
Posts: 61
Rep Power: 1
![]() |
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 |
|
|
|
|
|
#24 |
|
Programmer
Join Date: Nov 2007
Posts: 61
Rep Power: 1
![]() |
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. |
|
|
|
|
|
#25 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,722
Rep Power: 5
![]() |
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():. |
|
|
|
|
|
#26 |
|
Programmer
Join Date: Nov 2007
Posts: 61
Rep Power: 1
![]() |
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. |
|
|
|
|
|
#27 |
|
Expert Programmer
|
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.
|
|
|
|
|
|
#28 |
|
Programmer
Join Date: Nov 2007
Posts: 61
Rep Power: 1
![]() |
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. |
|
|
|
|
|
#29 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,722
Rep Power: 5
![]() |
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. |
|
|
|
|
|
#30 |
|
Programmer
Join Date: Nov 2007
Posts: 61
Rep Power: 1
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|