Reputation:
I’m trying to write a guessing word program in Python 2.7. The user has 50 chances to correctly guess the word. I’ve already written a function that generates a random word from a list of words. The user needs to correctly guess the word.
For example, if the correct word is “dog” and the user guesses “apple,” the program will return “too low” and the user will then guess again. If the user guesses “pear,” the program will return “too high.”
I don’t really know how to start writing this piece of code. I tried using a while loop, but I don’t know where to go from here. Thank you all in advance.
Here’s what I have so far:
Note: ‘correct’ is the word the user is trying to guess; guess is the word the user guessed
while guess != 'correct':
if guess < correct:
print 'Too low'
guess = int(raw_input('Enter a word')
elif guess > correct:
print 'Too high'
guess = int(raw_input('Enter a word')
else:
print 'You finally got it!'
break
print
while tries < 50:
guess = raw_input('Guess a word')
if guess not in word:
print "Sorry, try again."
else:
print 'Good job!'
else:
print "Sorry. My word was ", word, ". Better luck next time!"
Upvotes: 2
Views: 208
Reputation: 868
I like OmO Walker's answer to the question. Here is a slight variation of OmO's code.
# I like adding a .lower() so the person doesn't have to worry about the case matching.
correct = 'dog'.lower()
lives = 2
guess = None
# I like doing a > check not an != because stuff happens and if lives become < 0 you
# get stuck in an infinite loop. BUT in some cases comparisons <, > can be slower than ==.
# So if speed is a problem stick to ==
while not guess == correct and not lives <= 0:
# Moving this here makes it so there are fewer places you have to get input.
# Prints how many lives the user has left.
print "{} lives remaining".format()
guess = raw_input('Guess a word: ').lower()
if guess < correct:
print '{} was too low'.format(guess)
elif guess > correct:
print '{} was too high'.format(guess)
lives -= 1
if not lives > 0 and not guess == correct:
# I like using the format string method instead of adding strings or using ',' in a print. It is more reliable if
# the variable you are trying to print is not a string.
print "Sorry. My word was {}. Better luck next time!".format(correct)
# Makes sure the guess is really right before giving the confirmation
elif guess == correct:
print "You got it! The word was {}".format(correct)
# This is an added safety that will be triggered if some how the other conditions fall through. Its better to
# have an error thrown than have a program silently break.
else:
print "ERROR. Something is broken"
Upvotes: 2
Reputation: 646
there is no need for 2 loops. You can use the AND this will stop your loop if a person guessed the word right or if that person run out of lives. Also there is no need to say int() for your input as you only input a word. If the word is "too low" you print it decrease lives and enter a new word, similarly for "too high". You stop when your lives are at 0 or you guessed the word
guess = raw_input('Guess a word: ')
correct = 'dog'
lives = 50
while guess != correct and lives != 0:
if guess < correct:
print 'Too low'
lives -= 1
guess = raw_input('Enter a word: ')
else:
print 'Too high'
lives -= 1
guess = raw_input('Enter a word: ')
if lives == 0 and guess != correct:
print "Sorry. My word was", correct,". Better luck next time!"
else:
print "You finally got it!"
Upvotes: 2