How do I allow the user to guess the letter and corresponding number, hangman style?

Illumiknight07

I have a working program that:

Takes a phrase and encodes it with randomized numbers from 1-26 Allows user to get 3 letters, of their choice, and their computer assigned number But what I would like to do is allow the user to be able to guess a letter and what they think the correct letter is, if their guess is correct allow them to carry on but if it's wrong, to say so and let them try again.Eventually allowing them to guess the whole phrase as well.

Hopefully that makes sense :)

Here's the Code:

import string, random
import pandas as pd
#User Input
title = input("Please Enter A Title For This Puzzle: ")
if len(title) == 0:
    print("String is empty")
    quit()

phrase = input("Please Enter A Phrase To Be Encoded: ")
if len(phrase) == 0:
    print("String is empty")
    quit()


if not  phrase.islower():
    print("Please use lowercase for the phrase")
    quit()

#Numbers get assigned to the letters
nums = random.sample(range(1, 27), 26)
code = dict(zip(nums, string.ascii_lowercase))

print(    )
#Giveaway letters
num1 = int(input("Enter the first giveaway letter you would like (Note:a=0, b=1 ect): "))
num2 = int(input("Enter the second giveaway letter you would like (Note:a=0, b=1 ect): "))
#Code for Grid
code2 = {'Number': [[nums[num1]],[nums[num2]]],
        'Letter': (string.ascii_lowercase[num1],string.ascii_lowercase[num2]),
        }
#'Start' of Puzzle for the user  
print (  )
print ("The Title Of This Puzzle Is", title)
print(    )

df = pd.DataFrame(code2, columns = ['Number', 'Letter'])
code = dict(zip(string.ascii_lowercase, nums))
code.update({" ":100})
encoded = [code[item] for item in phrase]



print (df)
print(    )
print ("Hint: 100 is always a space")
print (encoded)

Note: I have asked this question before however the link provided wasn't that helpful, in this particular situation. An example or slight snippet of code would be appreciated.

I have been trying on my own but it looks like a mess and wouldn't work with this program. Regardless here it is:

def make_a_guess():

          print("A is " + str(a))
          print("Make a guess...")
          Article_Guess = input("What letter: ").lower()
          Numerical_Guess = int(input("What number: "))

          if Article_Guess == 'a' and Numerical_Guess == a:
               print_letters()
               print("Correct, A: " + str(a))

          elif Article_Guess == 'b' and Numerical_Guess == b:
               print_letters()
               print("Correct, B: " + str(b))

          elif Article_Guess == 'C' and Numerical_Guess == c:
               print_letters()
               print("Correct, C: " + str(c))

          elif Article_Guess == 'D' and Numerical_Guess == d:
               print_letters()
               print("Correct, D: " + str(d))

          elif Article_Guess == 'E' and Numerical_Guess == e:
               print_letters()
               print("Correct, E: " + str(e))

          elif Article_Guess == 'F' and Numerical_Guess == f:
               print_letters()
               print("Correct, F: " + str(f))

          elif Article_Guess == 'G' and Numerical_Guess == g:
               print_letters()
               print("Correct, G: " + str(g))

          elif Article_Guess == 'H' and Numerical_Guess == h:
               print_letters()
               print("Correct, H: " + str(h))

          elif Article_Guess == 'I' and Numerical_Guess == i:
               print_letters()
               print("Correct, I: " + str(i))

          elif Article_Guess == 'J' and Numerical_Guess == j:
               print_letters()
               print("Correct, J: " + str(j))

          elif Article_Guess == 'K' and Numerical_Guess == k:
               print_letters()
               print("Correct, K: " + str(k))

          elif Article_Guess == 'L' and Numerical_Guess == l:
               print_letters()
               print("Correct, L: " + str(l))

          elif Article_Guess == 'M' and Numerical_Guess == m:
               print_letters()
               print("Correct, M: " + str(m))

          elif Article_Guess == 'N' and Numerical_Guess == n:
               print_letters()
               print("Correct, N: " + str(n))

          elif Article_Guess == 'O' and Numerical_Guess == o:
               print_letters()
               print("Correct, O: " + str(o))

          elif Article_Guess == 'P' and Numerical_Guess == p:
               print_letters()
               print("Correct, P: " + str(p))

          elif Article_Guess == 'Q' and Numerical_Guess == q:
               print_letters()
               print("Correct, Q: " + str(q))

          elif Article_Guess == 'R' and Numerical_Guess == r:
               print_letters()
               print("Correct, R: " + str(r))

          elif Article_Guess == 'S' and Numerical_Guess == s:
               print_letters()
               print("Correct, S: " + str(s))

          elif Article_Guess == 'T' and Numerical_Guess == t:
               print_letters()
               print("Correct, T: " + str(t))

          elif Article_Guess == 'U' and Numerical_Guess == u:
               print_letters()
               print("Correct, U: " + str(u))

          elif Article_Guess == 'V' and Numerical_Guess == v:
               print_letters()
               print("Correct, V: " + str(v))

          elif Article_Guess == 'W' and Numerical_Guess == w:
               print_letters()
               print("Correct, W: " + str(w))

          elif Article_Guess == 'X' and Numerical_Guess == x:
               print_letters()
               print("Correct, X: " + str(x))

          elif Article_Guess == 'Y' and Numerical_Guess == y:
               print_letters()
               print("Correct, Y: " + str(y))

          elif Article_Guess == 'Z' and Numerical_Guess == z:
               print_letters()
               print("Correct, Z: " + str(z))
lucidbrot

I have not used pandas dataframes before. I'm sure you could also use those, but I find it easier to use the builtin structures.
What I recommended in the comments was using a list of characters. That could look like this:

mylist = [ 'a', 'b', 'c' ]
print(mylist[1])
# outputs 'b', because that's at position 1 in the list

However, since you already have a dict of the numbers and characters in your code, we can just as well use that code dict:

import string, random

def make_a_guess(solution):
    print("Make a guess...")
    letter = input("What letter: ").lower()
    number = int(input("What number: "))

    # the -1 is a default value to be returned if no valid number was provided. None would work just as well, if not better.
    if solution.get(number, -1) == letter:
        print("Correct, {} = {}".format(letter, number))
    else:
        print("Wrong, {} != {}".format(letter, number))

#User Input
title = input("Please Enter A Title For This Puzzle: ")
if len(title) == 0:
    print("String is empty")
    quit()

phrase = input("Please Enter A Phrase To Be Encoded: ")
if len(phrase) == 0:
    print("String is empty")
    quit()


if not  phrase.islower():
    print("Please use lowercase for the phrase")
    quit()

#Numbers get assigned to the letters
nums = random.sample(range(1, 27), 26)
code = dict(zip(nums, string.ascii_lowercase))

print('')

while True:
    make_a_guess(code)

Of course, you'll still have to add a stopping condition and a way to allow the user to enter the correct phrase as numbers. But the make_a_guess function should be what I think you were looking for.


Bonus Question

As you asked in a comment if I have an idea why the numbers from my code are off by one compared to the pandas indexing. That is likely simply due to this line here, which samples from a minimum of 1 instead of 0 up to but exclusive 27.

nums = random.sample(range(1, 27), 26)

If you change that to the following, it will also start at 0.

nums = random.sample(range(0, 26), 26)

Normally, arrays count from 0, not from 1, and it seems pandas keeps to this convention.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Trying to create a guess my letter code. How do I incorporate the case where the input char from the user equals my letter?

How do I display a message that tells the user how far off their guess was from the randomly generated number?

How do i count the number of attempts the user took to guess the correct sequence

replace underscore with correct guess letter, Python hangman

How would I get the letter corresponding to the number of occurences to sort?

How do i make the user input a number, and if the user inputs a letter it returns an error - VB.net

How do I loop the code to play again the guess number?

How do I convert a number to a letter in Java?

How do I convert number to a letter in Java?

How do I allow any character that is not a letter of the alphabet to be left as is in java

How to replace a letter in a string with its corresponding number?

I'm trying to make a program in C in which you can play hangman but as soon as it prints the option to guess the letter the program terminates

How do I display the input of the user, if the user guesses correctly in hangman c#

Hangman Issue: How do I make my loop reset when the letter entered is not present and how do I make the letter appear more than once in the word?

Ruby on Rails: How can I allow a user to style text on a form?

How can I ask a question over and over until the user guess's the number

How do I style text input by a user?

How can I make one guess activate multiple letters in a Hangman game?

How to generate a random number after user guess the correct number?

How do I convert letters to their corresponding number value?

How do I display each number with corresponding value?

How do I replace the user inputed integer with the corresponding String

How do I match something that is not a letter or a number or a space?

How do I find a combination of a letter a number in a VBA string?

How do I match a number not followed by a letter except for a certain substring

How do I convert a string to a number in R if the string contains a letter?

How do I slice a string in python based on the text, not the letter number?

How do I identify in a string that a letter has a number beside?

How do I compare user input to an uppercase letter string in a list?