Hello, I started to learn Python a few days ago in my attempt to learn programing. Before I move on with the book I want to understand something. I have learned enough to get in to trouble at this point. I created a number generator that you had to guess and a 'while' loop until the right answer is selected. I created this without the books help and it works but I don;t know why : )
here is the code...
# number game while in while loop
import random
num = int(raw_input("\nGuess: "))
cpu_guess = random.randrange(3) +1
while num != cpu_guess:
num = int(raw_input ("\n Wrong Guess Again: "))
cpu_guess = cpu_guess
print "Lucky Guess, it was", cpu_guess
..... Here is my question. Start of code I set up a variable called 'num' . but in order for it to work right I had to create another 'num' variable in the 'while' loop body. Does this 'num' variable over ride the first 'num' variable because it is in a loop? Or is it the same variable in both places that is being assigned the new number?
I hope I explained it OK. I only got the book last Wednesday but I want to understand what is happening before I move on. This example was not in the book but I used what I learned to create it. I just need to understand why it is working before I move on.
thanks!
-Lars
here is the code...
# number game while in while loop
import random
num = int(raw_input("\nGuess: "))
cpu_guess = random.randrange(3) +1
while num != cpu_guess:
num = int(raw_input ("\n Wrong Guess Again: "))
cpu_guess = cpu_guess
print "Lucky Guess, it was", cpu_guess
..... Here is my question. Start of code I set up a variable called 'num' . but in order for it to work right I had to create another 'num' variable in the 'while' loop body. Does this 'num' variable over ride the first 'num' variable because it is in a loop? Or is it the same variable in both places that is being assigned the new number?
I hope I explained it OK. I only got the book last Wednesday but I want to understand what is happening before I move on. This example was not in the book but I used what I learned to create it. I just need to understand why it is working before I move on.
thanks!
-Lars