Reputation: 65
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))
Upvotes: 1
Views: 52
Reputation: 6156
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.
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.
Upvotes: 2