-2

This is the task I was given to complete at first..

Guessing a letter A-Z.

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 ​

I made this;

def check_guess(guess="input" , letter="g"):

    guess=input("enter input for guess: ")

    if guess.isalpha()==False:

        print("invalid" , guess.isalpha())

    elif guess>letter:

       print("High")

    elif guess<letter:

       print("Low")

    else:

        print(guess==letter)

​ Now I was asked to do this;

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. ​

markwalker_
  • 12,078
  • 7
  • 62
  • 99
  • What's the actual problem you're having? Is there an error or unexpected output? – markwalker_ Mar 25 '18 at 09:12
  • no.. the code i mentioned above was perfect.. but i was asked to use that function "check_guess()" in there next problem i just mentioned below the code. i just cant make that program using the function i created above – Arunava Chakraborty Mar 25 '18 at 09:22

2 Answers2

1

I guess you are pretty new to the platform just like me, but you have to structure your questions a bit more clearly and ask specific questions instead of demanding us to solve your problem. What are you struggling with? I mean you wrote already what you need in text, all you have to do now is to research the corresponding solutions in python. What you need is basically:

# function to determine matches

def ismatch(letter, guess):
    if (letter == guess):
        return True
    else:
        return False

# function to determine if input is valid (do some research or write your 
# own by storing all valid characters somehow and checking if input is in 
# it, check: # https://stackoverflow.com/questions/15558392/how-to-check-if- 
# character-in-string-is-a-letter-python

def isalpha(guess):
    if (guess.isalpha()):
        return True
    else:
        return False

# Main programm (check how to use loops)
# create a counter for amount of guesses
# save the input for the letter to be guessed after checking if it's valid 
# otherwise restart input for the letter (or exit program if you like)
# save the input of the guess after checking if it's valid 
# otherwise restart input for the guess(or exit program if you like)
# compare if guess was right with your match-function
# if so, finish programm
# otherwise raise the guess_counter by 1
# if limit of guess counter ( in your case 3) reached, exit program
V.Diezel
  • 89
  • 4
0

You should use raw_input to read user input as a string. As per your requirement, you can try this code

def check_guess(guess="a", letter="g"):
    guess = raw_input("enter input for guess: ")
    if guess.isalpha()==False:
        print("invalid" , guess.isalpha())
    elif guess>letter:
       print("High")
    elif guess<letter:
       print("Low")
    else:
        print(guess==letter)
        return True
    return False

def letter_guess():
    input_letter = raw_input("Enter the Answer letter for match: ")
    for x in range(3):
        status = check_guess(letter=input_letter)
        if status is True:
            return True
    return False

if __name__ == '__main__':
    letter_guess()

Hope this helps!

binaryuser
  • 727
  • 1
  • 5
  • 13
  • instead of using `raw_input` can i use `str()` to just define the user input into a string right away? – Arunava Chakraborty Mar 25 '18 at 09:51
  • I actually ran the code in different python version. That's why it's not possible in *Python 2.7* but in *Python 3* using just `input()` assumes that it's a string, so nothing to worry over there. – binaryuser Mar 25 '18 at 09:58
  • but when i am running your code. it reapitedly is giving errors saying raw_input() name isnt defined. what to do? – Arunava Chakraborty Mar 25 '18 at 10:04
  • 1
    your code ran good. it asked for "Enter the answer letter for match:" then it asked for input saying "enter input for guess:" so here if my input dosnt match it asks for input 2 more times but at the end dosnt retun False. how to do this now? – Arunava Chakraborty Mar 25 '18 at 10:07
  • I assume that you're not facing any error now. In *Python3* `raw_input` has been removed, just replace it with `input()` method. – binaryuser Mar 25 '18 at 10:10
  • if you dont mind me asking, can i ask you for any of you social media accounts.. i mean really i am not only new to programming but also new to this site. this site is cool but i guess i will have sevaral more problems related to python and i would love to ask you directly for some help! it will be great for me! – Arunava Chakraborty Mar 25 '18 at 10:16
  • Please accept the answer if fits your requirement. I'm afraid that won't be possible as I don't presently have any active account on any social media. Bookmark this page for your python learning journey: https://github.com/rasbt/python_reference All the best !! – binaryuser Mar 25 '18 at 10:45