First off I want to really thank you for all of the help! I am in my first semester as I started as a non traditional student in January. Even though I am in an intro class I think that many of my class mates had more of a c/s foundation in high then I did. It took me a few tries but I did finally get my program to run using this code!!!! :)
#import random target = random.randint(1,100) 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. Is there an easier way or something that I am not getting? After doing that several times I just opened a new window and started from there without using the shell and it worked fine, so can I always do it that way and not use the shell at all? 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? Thanks again for all the help...Steve really helped by explaining it simply to me and I really appreciate it! It took me like 10-15 tries but I was so excited when I finally got it to run! On 2/24/12, Steven D'Aprano <st...@pearwood.info> wrote: > Carolina Dianne LaCourse wrote: > > [...] >> I understand that I need to ask for raw input from the user and that I >> need to be able to use the if elif else to tell the user whether their >> number matches or id too high or to low but am just not sure what to >> do first. Any advice would be greatly appreciated! I have tried some >> online tutorials to get the basics but still am having a really hard >> time. I did work with scratch a bit earlier this semester and got >> that, but am really struggling with python. > > Start by writing down in plain English the steps of how you would play the > guessing game. This is a called an algorithm, which is something very > similar > to a recipe or a set of instructions. You might have something like this: > > (1) Think of a number between 1 and 100, and remember it. > (2) Repeat the following steps until the game is over: > (3) - Ask the person playing for a number between 1 and 100. > (4) - If the number is too low, tell them it is too low. > (5) - If the number is too high, tell them it is too high. > (6) - If the number is equal to the number you thought of, the game is over. > > All that makes up *one* game. Then you need instructions to play multiple > games: > > (a) Play one game, as above. > (b) Repeat the following steps until done: > (c) - Ask the player if they want to play again. > (d) - If they say Yes, play one game, as above. > (e) - Otherwise, we are done. > (f) Finally, print how many games were played, how many guesses were needed, > and the average number of guesses per game. > > > Now, you need to change the English instructions to instructions the > computer > can follow, using Python. For example, Step (1) above picks a random number > and remembers it as the target of the game: > > import random > target = random.randint(1, 100) > > Step (2) is a bit harder -- it's a loop. You should have learned about while > loops and for loops. I expect a while loop is better for this, because you > can't tell ahead of time how many times you need to go round and round the > loop. > > > while guess != target: > Step (3) ask the user for a number, and call it "guess" > if guess < target: > print "too low" > elif guess > target: > print "too high" > # otherwise guess == target so the game will be over > > > Notice that this isn't exactly Python code. The most obvious problem is the > line "Step (3)..." which is plain English. You need to replace that with > code > to actually ask the user for a number. (Hint: you will need the raw_input > function.) > > Another problem is that the *first* time you enter the loop, the name > "guess" > isn't defined. You need to give it a value to start with, before the loop. > Any > value will do, so long as it isn't target. I suggest 0. > > Does this help you get started? Go ahead and write some code, and see where > it > takes you. Piece by piece, step by step, you should work towards replacing > each bit of English instruction with some Python code to do that. > > You should aim to write code to play *one* game first. Get that right, > first, > then adapt it to play multiple games. > > Write some code, see how it works (or where is fails to work), and anything > that is unclear, come back and ask. > > > > -- > Steven > > _______________________________________________ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor