-1

So, here is the task. Can't solve it, please help.

check_guess() takes 2 string arguments: letter and guess (both expect single alphabetical character) - if guess is not an alpha character print invalid and return False - test and print if guess is "high" or "low" and return False - test and print if guess is "correct" and return True

Letter Guess

create letter_guess() function that gives user 3 guesses

takes a letter character argument for the answer letter gets user input for letter guess calls check_guess() with answer and guess End letter_guess if check_guess() equals True, return True or after 3 failed attempts, return False

First of all I can't make solution of 3 tries. Second problem is that I can't make error when I input digits for ex.

letter = "J"
tries = 3

guess = input ("Enter your guess ")


def check_guess (guess, letter):


if letter == guess.upper():
    print ("correct")
    True
    return 

elif letter < guess.upper():
    print ("You are wrong, but go closer to A")
    False
    return 
elif letter > guess.upper():
    print ("You are wrong, but go closer to Z")
    False
    return 

def letter_guess (guess, letter, tries):
if check_guess (guess, letter) == True:
    pass
elif check_guess (guess, letter) == False:
    tries - 1
    return

if tries == 0:
print ("GAME OVER!")
else:
check_guess (guess, letter)
Anton Zubochenko
  • 153
  • 1
  • 11
  • 1
    You're not actually returning your boolean values. `False` or `True` on their own are not doing anything, and `return` on its own is returning `None`. `return False` or `return True` would return the bools though. Please also fix your indentation. – roganjosh Jun 08 '18 at 16:21
  • 1
    Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. You've posted two independent problems, and the code you give doesn't run at all. – Prune Jun 08 '18 at 16:59

3 Answers3

1

You are not using the letter_guess function and you're not returning a result (other than None) from the check_guess function.
Try this instead:

letter = "J"
tries = 3

def check_guess (guess, letter):
    if not guess.isalpha():
        print("Invalid")
        return False
    if letter == guess.upper():
        return True
    elif letter < guess.upper():
        print ("Your guess is High")
        return False
    else:
        print ("Your guess is Low")
        return False

def letter_guess():
    for i in range(tries):
        guess = input ("Enter your guess ")
        res = check_guess (guess, letter)
        if res:
            print ("Correct!")
            return True
    return False

result = letter_guess()
if result:
    print ("Congratulations")
else:
    print ("The answer was ",letter)
print ("GAME OVER!")
Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60
0

Your code can be simplified by getting rid of the letter_guess function and condensing all of the game logic into one function.

Then to solve the problem of doing 3 tries, I made a while loop that loops until tries == 0. When the loop hits a statement where the guess was wrong it subtracts one try.

I also had to move the

guess = input ("Enter your guess ")

into the while loop so it will iterate multiple times and get input for three different tries.

correctLetter = "J"

def check_guess (letter):

    tries = 3

    while tries > 0:

        guess = input ("Enter your guess ")

        if letter == guess.upper():                
            print ("Correct!")
            break

        elif letter < guess.upper():
            if tries > 1:
                print ("You are wrong, but go closer to A")
            tries = tries - 1    #Moves you closer to finishing the loop

        elif letter > guess.upper():
            if tries > 1:
                print ("You are wrong, but go closer to Z")  
            tries = tries - 1  #Moves you closer to finishing the loop

    if tries == 0:
        print ("GAME OVER!")

check_guess(correctLetter)

Side note: to return any value from a function, that value needs to directly follow the return statement like so:

return True

I hope that helps!

Austin B
  • 184
  • 1
  • 1
  • 8
0

This accomplishes the same thing but with using two different functions.

correctLetter = "J"
tries = 3

def letter_game(tries, correctLetter):
    for i in range(tries):

        guess = input ("Enter your guess ")

        if letter_check(correctLetter, guess) == True:
            print("Correct!")
            break
    if letter_check(correctLetter, guess) != True:
        print("Game Over!")

def letter_check(l1, l2):
    if l1 == l2.upper():
        return True    
    elif l1 > l2.upper():
        print ("You are wrong, but go closer to Z")
        return False
    elif l1 < l2.upper():
        print ("You are wrong, but go closer to A")
        return False
    else:
        return False

letter_game(tries, correctLetter)

The function letter_game runs the game, and the letter_check function returns a Boolean value corresponding to whether or not the guessed letter matches the correct letter.

Austin B
  • 184
  • 1
  • 1
  • 8