Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie (Simple Program help)

Status
Not open for further replies.

exit12

Technical User
Aug 19, 2003
44
GB
Hi all

I'm new to Python and working through some tutorials from my books.

I'm trying to write a simple program which the user has to guess a number, and it will say if they are to high or to low. I would like the program to stop after the user has had say 4 guesses and not allow them to guess anymore. I have tried to do this but when they get to 4 shots, it still lets them continue...anyone please give me some pointers?

Thank You.

My code is below.

number = 50
guess = 0
shots = 0

while guess != number:
shots=shots + 1
guess = input("Guess a Number ")

if shots == 4:
print "Guesses Over, you've had 4 shots"

elif guess > number:
print "Too High"

elif guess < number:
print "Too Low"

print "You guessed Correctly!
 
Just like when you print "Too High" or "Too Low" the loop continues there's no reason for the loop to stop looping when shots == 4.

One way to make it stop looping would be to use the "break" directive.
Another way would be to check the number of guesses in the while conditional by saying something like:

while guess != number and shots < 4:

It would take a little rearranging of code inside the loop, but it'll be a good exercise.

Good luck.
 
Thanks for the quick reply! Much appreciated.

I have this so far...

number = 50
guess = 0
shots = 0

while guess != number and shots < 4:

guess = input("Guess a Number ")

if guess > number:
shots=shots +1
print "Too High"

elif guess < number:
shots=shots +1
print "Too Low"


print "Game Over"

This is my sirst attempt at programming so simple things will not be simple to me! :), This prints game over, regardless if they get it right or have 4 guesses, so the user won;t know if they got it right or used all their guesses. Is there a way to print out a different message when they guess the number? and just to print game over when there gueses are up? Did that make sense?

is there an easier or "rule of thumb" way of doing this exercise, or am I on the right track?

Thanks
 
This isn't really a python question, but more one of general program structure.

There's a couple ways to approach it.

One way would be looping forever, then printing an appropriate message and exiting the loop when one of any number of exit criteria are met:

Code:
shots = 0
number = 30
while True:
  guess = input()
  if guess > number:
    print "too high"
  elif guess < number:
    print "too low"
  else:
    print "that's right, you win"
    break
  shots = shots + 1
  if shots > 4:
    print "too many guesses, you lose"
    break

print "Game Over"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top