1. ## Python Assistance Needed

Hello All, this is basically my first tackle on programming in itself. I chose python, because it seems pretty straight-forward for now. I'm on HtRegz's 2nd tutorial now, and am feeling like i'm understanding it and progressing just fine, but the while loop is just a little difficult for me to understand at first, so here is where i might need a little help

Code:
```from random import *
answer = randint(1,25)         # I'm aware this assigns a random number of 1-25 to the var 'answer'
guess = 0
counter = 0                         # I'm having a hard time figuring out how the counter works
print "Welcome to the Number Guessing Game"
guess = input("Please Guess a Number between 1 and 25: ")
print "To low."
print "To high."
counter = counter + 1
print "Congrats you guessed the correct number (",answer,") in",counter,"tries."
# I know they are are random numbers, but if you guess the right number, the counter goes up 1, but  i don't understand why it goes up to 1, because there wasn't a statement anywhere that said they were equal to each other.```
Now I feel like i answered my own question

little help here ...

2. Whats wrong with it? your program works fine...

instead of using counter, can you not just use guess? notice that if you do this:
Code:
`print "Congrats you guessed the correct number (",answer,") in",counter,"tries.", guess`
that guess will be the same as counter,

other than that what are you trying to get? are you confused by the fact that the while loop has nothing at the end such as in C:
Code:
```while(x &lt; y)
{

}```
As I was when I first learnt python I didnt understand the concept of the indentation, which led to many hours or debugging...

let use no in more detail what your struggling with..

i2c

3. I don't know Python, and I don't do programing, but here is what I see:

First, I did not try it, I am assuming it works as you said. I also assumed what i2c said about the the indentation. I will only include the parts that I believe will answer your question.

counter = 0 # sets the counter to zero

while guess != answer : # starts the loop: note here, the first time through this is always not equal because you haven't guessed a number yet. First time through guess=0, answer is &gt;0. Now you are in the loop.
guess = input("Please Guess a Number between 1 and 25: ") # here you input your guess
print "To low."
# if guess too low, it prints this
print "To high."
# if guess too high, it prints this
counter = counter + 1 # counter gets 1 added to it, it restarts the loop ( if guess was correct it still goes back to beginning, but then breaks out of the loop because guess is equal to answer, so it then jumps below the loop )

Did you follow that? Does that explain your question on why the counter incremented even if you guessed the correct answer the first time? ( I think that was your question )

Anyone, did I get that right?

4. Hey Hey,

IKnowNot described what is happening perfectly and as i2c said the indentation is crucial.

Here's sample output

D:\ACTIVE~1\Python24&gt;python test.py
Welcome to the Number Guessing Game
Please Guess a Number between 1 and 25: 10
To low.
Please Guess a Number between 1 and 25: 15
To high.
Please Guess a Number between 1 and 25: 13
To low.
Please Guess a Number between 1 and 25: 12
To low.
Please Guess a Number between 1 and 25: 14
Congrats you guessed the correct number ( 14 ) in 5 tries.

I have to wonder though i2c, why you think that guess and counter will be the same number, if we add guess to the end we get

D:\ACTIVE~1\Python24&gt;python test.py
Welcome to the Number Guessing Game
Please Guess a Number between 1 and 25: 10
To high.
Please Guess a Number between 1 and 25: 5
To low.
Please Guess a Number between 1 and 25: 7
Congrats you guessed the correct number ( 7 ) in 3 tries. 7
Guess = 7 and Counter = 3... The only time they would be equal is if by coincidence. Guess = Number Guessed and not the number of guesses.

I'm on my way to work, but either while I'm there or when I get back I'll throw together some small examples of the while command.

Peace,
HT

5. Yea sorry my mistake, I didnt read code correctly, if you guess sequentially (like i did) ie: - 1,2,3,4,5,6, guess is +1 (or the same if you miss guess 1 and then carry on with 1,2,3,4...) more than counter, thats what led me to make that brash statement,

i2c

6. Originally posted here by i2c
Yea sorry my mistake, I didnt read code correctly, if you guess sequentially (like i did) ie: - 1,2,3,4,5,6, guess is +1 (or the same if you miss guess 1 and then carry on with 1,2,3,4...) more than counter, thats what led me to make that brash statement,

i2c
makes sense.. I was a lil confused at first.

Peace
HT

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

×