Jump to content
  • Advertisement
Sign in to follow this  
Sobel

Python High_Low Game

This topic is 3627 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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. :(

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites

tries = 0
number = 78
guess = 0

while guess != number:
guess = input ("Guess a number: ")
tries = tries + 1
if guess > number:
print "Too high"
elif guess < number:
print "Too low"

if tries > 3:
print 'That must be complicated.'
print "Just right"


Share this post


Link to post
Share on other sites
Quote:
Original post by monp
*** Source Snippet Removed ***


Great, thank you monp :)

Share this post


Link to post
Share on other sites
There isn't anything wrong now (except still for indentation)?

Now make it a bit more fun. Add this:


import random

#number = 78
number = random.randint(1, 100)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!