Hi there,
I am learning python from scratch, trying to solve a 'task', having some issues.
The task is:
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
I've tried many different things, but I think I struggle with building the function correctly. I would really appriciate if someone could show me how they would write this correctly so I can learn and apply it for the next tasks.
Thanks in advance!
(Aug-21-2019, 02:50 PM)Kristen Wrote: [ -> ]I've tried many different things, but I think I struggle with building the function correctly
Please, post your best attempt at solving this task. Post your code in python tags, full traceback (if you get any errors) + in error tag.
(Aug-21-2019, 03:06 PM)buran Wrote: [ -> ] (Aug-21-2019, 02:50 PM)Kristen Wrote: [ -> ]I've tried many different things, but I think I struggle with building the function correctly
Please, post your best attempt at solving this task. Post your code in python tags, full traceback (if you get any errors) + in error tag.
Hi,
Thanks for the reply. This is my best attempt, there are planty of things wrong with this:
answer = "R"
def letter_guess(check_guess):
first_attempt = input("Guess a letter: " )
second_attempt = input("Try again: " )
third_attempt = input("And again: ")
if first_attempt.upper() in answer == True:
print("True")
elif second_attempt.upper() in answer == True:
print("True")
elif third_attempt.upper() in answer == True:
print("True")
else:
print ("False")
letter_guess(check_guess)
Its not exactly what the instructions are saying to do, in addition to getting the output "False" even when the guess is "r" which is the answer.
Would appriciate if you could show me how you would write this, I think I am also not entirely understanding how to build these functions correctly.
Thank you.
With this pseudo code you should be able to understand what the exercise wants you to do
def check_guess(answer, guess):
depending on whether answer and guess are equal or not leave this function with a return value True or False
def letter_guess(answer):
for 3 guesses from the user
guess = get an input from the user
if calling check_guess with arguments answer and guess is True
user has guessed correctly so leave letter_guess with a return value True
else inform user that guess was not correct
after 3 false guesses leave letter_guess with a return value False
answer = 'X'
correct = letter_guess(answer)
This is my take on how it could be done:
function letter_guess takes a single parameter that is a string representing the letter to be guessed.
- start a loop that will repeat 3 times.
- get the users input for the guessed letter.
- call the function check_guess passing it the letter to be guess and the guessed letter.
- if check_guess is True.
- The loop has ended after 3 attempts, return False.