Placing my integers into a list in python - python

I am creating a program that uses the Caesar cipher to encode and decode messages. So far I am building the groundwork, I am trying to get my list of characters the user gives me and make a list of integers using ord...So far all I am having trouble with is getting my integers that I have received back into a list.
import random
encode_decode = input("Do you want to encode, or decode? (e/d)")
shift_amount = input("Please enter the shift amount for your message")
if encode_decode == "e" or encode_decode == "E":
user_words_unrefined = input("Enter your message to encode!")
elif encode_decode == "d" or encode_decode == "D":
user_words_unrefined = input("Enter your message to decode!")
user_words_refined = list(user_words_unrefined)
Alphabet = [chr(i) for i in range(ord('a'), ord('z') + 1)]
Counter = 0
for i in range(len(user_words_refined)):
user_words_numbers = (ord(user_words_refined[Counter]))
user_numbers_list = [user_words_numbers]
print(user_numbers_list)
Counter += 1
Input ("Hello, Party people!")
output
it prints them all on seperate lines with square brackets around them...any ideas?
[72]
[101]
[108]
[108]
[111]
[44]
[32]
[80]
[97]
[114]
[116]
[121]
[32]
[112]
[101]
[111]
[112]
[108]
[101]

Instead of using a for loop that ranges over the length of the input string, you could use a list comprehension.
user_numbers_list = [ord(letter) for letter in user_words_refined]

This should sort you out. see the comments in the code. I will be happy to provide further explanation if need be
user_numbers_list = [] #initialise the list
Counter = 0
for i in range(len(user_words_refined)):
user_words_numbers = (ord(user_words_refined[Counter]))
user_numbers_list.append(user_words_numbers) #add to the end of the list
print(user_numbers_list)
Counter += 1
The best bet is actually list comprehension... see Jordan Lewis's answer for a neater approach

Change the last part to something like
user_numbers_list = []
Counter = 0
for i in range(len(user_words_refined)):
user_words_numbers = (ord(user_words_refined[Counter]))
user_numbers_list.append(user_words_numbers)
print(user_numbers_list)
Counter += 1
Also, consider iterating directly through user_words_refined, like
for word in user_words_refined:
user_words_numbers = ord(word)
user_numbers_list.append(user_words_numbers)
print(user_numbers_list)

Just as a side note, instead of
encode_decode = input("Do you want to encode, or decode? (e/d)")
you could use
encode_decode = input("Do you want to encode, or decode? (e/d)").lower()
so you don't have to use too many or's in your if statement.

Related

Looping over characters (and their indices) in Python [duplicate]

So I'm making a hanging man game and I have run into a problem regarding indexes. Basically, I want to find the index of a letter inside a secret word, the problem is that if the secret word includes two letters that are the same, for instance, "guacamole", where the letter a has the index of 2 and 4 but when I want to find the index of a, it only prints "2" and not "4". Is there a way around this? Thanks in advance!
Part of code where problem occurs:
for letter in secret_word:
if user_guess == letter:
current_word_index = secret_word.find(letter)
print(current_word_index) #Not in full program, only to test errors.
Full code:
#Hanging man
import string
space = "\v"
dbl_space = "\n"
secret_word = str(input("Enter a secret word: "))
guess_low = list(string.ascii_lowercase)
used_letters = []
user_errors = 0
user_errors_max = 1
secret_word_index = int(len(secret_word))
secret_word_placeholder = list(range(secret_word_index))
while user_errors != user_errors_max:
user_guess = str(input("Enter a letter: "))
if len(user_guess) != 1:
print("You have to pick one letter")
if user_guess in guess_low:
guess_low.remove(user_guess)
used_letters.extend(user_guess)
print(used_letters)
for letter in secret_word:
if user_guess == letter:
current_word_index = secret_word.find(letter)
if user_errors == user_errors_max:
print("You lost the game, the secret word was: " + secret_word)
This is an example of what you are trying to achieve. use list comprehension.
string='hello'
letter='l'
[idx for idx,ch in enumerate(string) if ch==letter]
Python's string find accepts a start parameter that tells it where to start searching:
>>> "guacamole".find('a')
2
>>> "guacamole".find('a', 3)
4
Use a loop, and use the index of the last hit you found + 1 as the start parameter for the next call.
Another more verbose solution might be:
str1 = "ooottat"
def find_all_indices(text, letter):
indices_of_letter = []
for i, ch in enumerate(text):
if ch == letter:
indices_of_letter.append(i)
return indices_of_letter
print(find_all_indices(str1, 'o'))
Side note:
Indexes is the nontechnical plural of index. the right technical plural for index is indices
Yes, if you instantiate a new_variable to be the secret_word variable before the for loop, the in the line current_word_index = secret_word.find(letter) change secret_word.find(letter) to new_variable .find(letter) then below the if statement write new_variable = new_variable [1:] which will remove the letter just found.
so your code would look something like:
new_variable = secret_word
for i in secret_word
if user_guess == letter:
current_word_index = new_variable.find(letter)
#I would do print(current_word_index) this will show the index
#Or current_word_index = current_word_index + ',' + new_variable.find(letter)
new_variable= new_variable[1:] #this will take away your letter.

python string.split() and loops

disclaimer im new to python
i need to split a string input send if to a function that substitutes a character in the string with a different character (like a substitution cipher) but i just dont know how to go about this
print('Welcome to the encryption protocol for top secret governemt cover ups')
string=input('whats your message?')
def encrypt(string):
alpha = "abcdefghijklmnopqrstuvwyz"
sub_alpha = "pokmenliuytrwqazxcvsdfgbhn"
index=0
while index < len(string):
letter=string[index]
im not really sure what im doing im really bad at python, this has had me stumped for 3 days now ive reviewed my course material and tried videos on youtube im probably just really really dumb
I think the key piece of knowledge you're missing is that strings are iterable. So you can do things like:
for c in "FOO":
print(c)
# prints "F\nO\nO\n"
And you can find the index of a character within a string with str.index. So you can build up your cyphertext like this:
alpha = "abcdefghijklmnopqrstuvwyz "
cypher = "pokmenliuytrw qazxcvsdfgbhn"
plaintext = "some string"
cyphertext = ""
for c in plaintext:
char_index = alpha.index(c)
cyphertext += cypher[char_index]
You can also iterate over things inline - this is called a comprehension. So to transform your string you can do this instead of using the for loop:
cyphertext = "".join(cypher[alpha.index(c)] for c in plaintext)
The example above uses the str.join function to concatenate each character of cyphertext.
Here is a solution that asks the question and then iterates through each letter, finding the index in the alpha key, and replacing it with the sub_alpha key equivalent.
Note this example also checks if it should be lowercase or uppercase.
EDIT: if the input character does not have a valid cipher, it doesn't get altered.
EDIT 2: expanded answer to convert both forwards and backwards.
alpha = "abcdefghijklmnopqrstuvwyz"
sub_alpha = "pokmenliuytrwqazxcvsdfgbhn"
def encrypt(in_char):
is_lower_case = in_char.islower()
index = alpha.find(in_char.lower())
if index < 0:
return in_char
elif is_lower_case:
return sub_alpha[index]
else:
return sub_alpha[index].upper()
def decrypt(in_char):
is_lower_case = in_char.islower()
index = sub_alpha.find(in_char.lower())
if index < 0:
return in_char
elif is_lower_case:
return alpha[index]
else:
return alpha[index].upper()
print('Welcome to the encryption protocol for top secret governemt cover ups')
input_str=input('whats your message? ')
output_str=""
for letter in input_str:
output_str += encrypt(letter)
print("Encrypted: ")
print(output_str)
input_str=""
for letter in output_str:
input_str+= decrypt(letter)
print("Decrypted: ")
print(input_str)

Formatting two lists into a single dictionary

I'm coding a hangman game for my Computer Science class, and I'm trying to create a dictionary that has each character of the word given and a 0 to notate whether it's been guessed or not.
gamestart = 0
guesses = 5
gamewin = 0
while gamestart == 0:
word = input("Welcome to hangman!" + "\nEnter a word: ")
if word.find(" ") > -1:
print("\nSorry Please use one word only!\n")
elif word.find(" ") == -1:
gamestart = 1
for i in range(len(word)):
wordspacing = [0] * i
wordstore = list(word)
wordstore = dict(zip(wordspacing, wordstore))
print(wordstore)
however when I try to put the two lists I have together I get the output:
Welcome to hangman!
Enter a word: word
{0: 'r'}
Could someone identify the reason for this happening. I would also like a little bit of criticism in terms of efficiency.
The problem is that you are making a dictionary of 0 to letters, and each key can only have one value.
Try just using a dictionary comprehension
wordstore = {letter: 0 for letter in word}
To achieve the desired output using your method you need to switch the order of the zipped objects
wordstore = dict(zip(wordstore, wordspacing))
Also for wordspacing, you don't want to continually assign a value to wordspacing, plus you only end up with [0,0,0] instead of [0,0,0,0] since the last value of i is 3 and not 4. So go with the following
wordspacing = [0] * len(word)

How to assign item to a string in python

I'm really new to programming in general, but I'm a fast learner. I've been working on a project. I wanted to make a simple hangman game, but I hit a road block and I'd like to figure it out before continuing.
I'm trying to assign correct guesses to an empty variable and print is as they go, but it seems I can't assign "items" to strings. Is there an alternative method I could use?
Here's the code
switch = True
def hangman():
guess_number = 0 # Var that keeps track of the guesses
secret_word = input("What is the secret word?\n>") # Gets the secret word
print("The secret word is %d characters long." % len(secret_word)) # Lenght of secretword
answer = "-" * len(secret_word) # Create empty answer for assigning characters
while switch is True:
guess_number = guess_number + 1 # Counts the guesses
index_num = 0 # Tring to use this to assign correct guesses to answer
user_guess = input("Guess #%d >" % guess_number) # Gets user guess
print("Secret word: " + answer) # prints empty answer as "----"
for each_char in secret_word:
index_num = index_num + 1 # Counting index for assigning to answer variable
print("testing index #" + str(index_num))
if user_guess is each_char:
print("Correct Guess for index #" + str(index_num))
#------> answer[index_num] = each_char <--------
hangman()
Python strings are immutable, they can't be modified.
You can treat your string as a list
answer = list("-" * len(secret_word))
Then join chars together
answer_str="".join(answer)
There are some other ways, that have been suggested. If you're determined to keep the string answer, try this:
answer = answer[:index_num] + each_char + answer[index_num+1:]
This creates a new string by adding (string add is concatenation) together three substrings: first, the substring created by slicing the original string from zero (default: [:) up to index_num, non-inclusive. That is, answer[0] ... answer[index_num-1]. Then the each_char, which is a string (or a char, same difference). Finally, another substring, this one from index_num+1 running up to the end (default: :]).

Selecting specific int values from list and changing them

I have been playing with Python and came across a task from MIT, which is to create coded message (Julius Cesar code where for example you change ABCD letters in message to CDEF). This is what I came up with:
Phrase = input('Type message to encrypt: ')
shiftValue = int(input('Enter shift value: '))
listPhrase = list(Phrase)
listLenght = len(listPhrase)
ascii = []
for ch in listPhrase:
ascii.append(ord(ch))
print (ascii)
asciiCoded = []
for i in ascii:
asciiCoded.append(i+shiftValue)
print (asciiCoded)
phraseCoded = []
for i in asciiCoded:
phraseCoded.append(chr(i))
print (phraseCoded)
stringCoded = ''.join(phraseCoded)
print (stringCoded)
The code works but I have to implement not shifting the ascii value of spaces and special signs in message.
So my idea is to select values in list in range of range(65,90) and range(97,122) and change them while I do not change any others. But how do I do that?
If you want to use that gigantic code :) to do something as simple as that, then you keep a check like so:
asciiCoded = []
for i in ascii:
if 65 <= i <= 90 or 97 <= i <= 122: # only letters get changed
asciiCoded.append(i+shiftValue)
else:
asciiCoded.append(i)
But you know what, python can do the whole of that in a single line, using list comprehension. Watch this:
Phrase = input('Type message to encrypt: ')
shiftValue = int(input('Enter shift value: '))
# encoding to cypher, in single line
stringCoded = ''.join(chr(ord(c)+shiftValue) if c.isalpha() else c for c in Phrase)
print(stringCoded)
A little explanation: the list comprehension boils down to this for loop, which is easier to comprehend. Caught something? :)
temp_list = []
for c in Phrase:
if c.isalpha():
# shift if the c is alphabet
temp_list.append(chr(ord(c)+shiftValue))
else:
# no shift if c is no alphabet
temp_list.append(c)
# join the list to form a string
stringCoded = ''.join(temp_list)
Much easier it is to use the maketrans method from the string module:
>>import string
>>
>>caesar = string.maketrans('ABCD', 'CDEF')
>>
>>s = 'CAD BA'
>>
>>print s
>>print s.translate(caesar)
CAD BA
ECF DC
EDIT: This was for Python 2.7
With 3.5 just do
caesar = str.maketrans('ABCD', 'CDEF')
And an easy function to return a mapping.
>>> def encrypt(shift):
... alphabet = string.ascii_uppercase
... move = (len(alphabet) + shift) % len(alphabet)
... map_to = alphabet[move:] + alphabet[:move]
... return str.maketrans(alphabet, map_to)
>>> "ABC".translate(encrypt(4))
'EFG'
This function uses modulo addition to construct the encrypted caesar string.
asciiCoded = []
final_ascii = ""
for i in ascii:
final_ascii = i+shiftValue #add shiftValue to ascii value of character
if final_ascii in range(65,91) or final_ascii in range(97,123): #Condition to skip the special characters
asciiCoded.append(final_ascii)
else:
asciiCoded.append(i)
print (asciiCoded)

Categories

Resources