> import random #import random number generator module > target = random.randint(1,100) #generates random number between 1 and 100 > guess = float(raw_input('pick a number between 1 and 100')) > while guess != target: > if guess < target: print 'too low' > elif guess > target: print 'too high' > #otherwise guess == target and game is over > > I am having trouble getting it to run for more than one game but am > trying. Also I started off using the idle shell to write the codes but > it wont let you run it from there, so I opened a new window and > retyped it for my first few attempts. I am unsure why you would use > the idle shell if you would need to open a new window and retype it > anyway. [snip] > so can I always > do it that way and not use the shell at all?
The shell is for testing out things; it's not really for writing entire programs in. But as a test-tool it's priceless. Don't know what random.randint() does? Use the shell to find out. Want to see if you can convert ints to floats? Use the shell. It's a great way to test short things like that, because it will give you instant feedback. You can write longer code in it, but it can be cumbersome. In the end, it's another tool for your belt, I guess you could say. > I am also still kind of > unsure about the loops and was hoping you could explain a little more > about it or may know of a good online resource? The while loop you're using tests for one thing: is guess == target. If it isn't, the contents of the while loop executes. I should note that while your loop works, it doesn't work as intended: what happens if you guess too low? Let's see how the program runs: computer picks a number between 1 and 100 and stores it in variable 'target' computer then asks user for a number between 1 and 100 and stores it in variable 'guess' while loop checks whether guess is the same as target. If it's not, it checks if the guess is higher than target or lower than target and then prints the corresponding response. the while loop checks again if guess == target. It's not, since we never change the value of guess. It checks if the guess is higher than target or lower than target...(etc) the while loop checks again... In short, you can say that the loop is named because of its behaviour: it loops until it shouldn't any longer. I'm sure you can see the problem here. So, while you now can check whether a variable is equal to another variable, you currently have no way to change 'guess' for a new try. Can you think of any way to solve that? And while this might not be entirely on topic, there's a wonderful book for complete beginners -- I've used it as well -- that I'd like to recommend: Python Programming for the Absolute Beginner. In fact, one of the excercises happen to be this particular game. -- best regards, Robert S. _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor