Skip to main content
GameDev.net gamedev.net
🔒 Locked

Python High_Low Game

Started by Sobel Jul 18, 2008 at 12:31 PM 7 replies 1.7k views
Original Post
Sobel
Sobel
I've been using this tutorial on learning Python http://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python/Contents It's a great tutorial, but I'm stuck on one of the exercises. Above they had me create a high_low game with simple code (I'll put it in below), and then in the exercise, they wanted me to take that code and modify it to keep track of how many times the player entered the wrong number. If the number is greater than three times, print "That must have been complicated." I'll show you the main code and the two ways I tried, both are wrong and could use some help on figuring out what my problem is. number = 78 guess = 0 while guess != number: guess = input ("Guess a number: ") if guess > number: print "Too high" elif guess < number: print "Too low" print "Just right" That's the original code number = 78 guess = 0 while guess != number: guess = input ("Guess a number: ") if guess > number: print "Too high" elif guess < number: print "Too low" elif guess > 3 print "That must have been complicated." print "Just right" That was my first attempt and after I ran it I saw the problem, if the number guessed was greater than 3, it would print "Too high" or "Too low" along with "That must have been complicated." number = 78 guess = 0 while guess != number: guess = input ("Guess a number: ") if guess > number: print "Too high" elif guess < number: print "Too low" print "Just right" if guess > 3 print "That must have been complicated." This was my second attempt and I thought it worked, but if you got the correct answer in 1 try it still printed "That must have been complicated." So now I'm stumped, what am I doing wrong? I've wracked my brain trying to figure it out, and I don't know where it is I'm going wrong. Does it have something to do with the guess = 0 at the beginning? *edit* The code was indented in the message, but apparently doesn't show up on the post. :(
oler1s
oler1s
Put CODE tags or SOURCE tags around code to preserve formatting and indentation.
Sobel
Sobel
Quote:
Original post by oler1s
Put CODE tags or SOURCE tags around code to preserve formatting and indentation.


How do you do that?

Sorry for all the questions, I'm new to both programming and this forum.

monp
monp
tries = 0number = 78guess = 0while guess != number:guess = input ("Guess a number: ")tries = tries + 1if guess > number:print "Too high"elif guess < number:print "Too low"if tries > 3:  print 'That must be complicated.'print "Just right"
Sobel
Sobel
Quote:
Original post by monp
*** Source Snippet Removed ***


Great, thank you monp :)

visitor
visitor
There isn't anything wrong now (except still for indentation)?

Now make it a bit more fun. Add this:

import random#number = 78number = random.randint(1, 100)
Jonathan5
Jonathan5
I was trying to figure out the exact same thing, but I was already using the random int. Something else I can't figure out though is how to make the program restart after they have finished playing and are prompted with the text: "Would you like to play again Y/N?" . I was told it had to do with loops, but still couldn't figure it out.
shrimpydude
shrimpydude
edit: sorry didnt see earlier post

[Edited by - shrimpydude on July 19, 2008 7:08:49 PM]
ibebrett
ibebrett
Hey guys, I was just flipping through the MIT open course ware (which is an excellent resource) and I noticed a course called -

A Gentle Introduction to Programming Using Python

Link:
http://ocw.mit.edu/OcwWeb/Electrical-Engineering-and-Computer-Science/6-189January--IAP--2008/CourseHome/index.htm

I remembered glancing at this thread and I figured I'd let you know... may be worth checking out.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.