Matt
Matt

Reputation: 55

Generating key characters and appending them to a string

I am currently working on a project with 'Encryption' at school, the first task is to do the following:

  1. import random
  2. Create an empty string variable named 'key'
  3. repeat 8 times
  4. Choose a random number between 33 and 162 (The ascii table)
  5. Take the chosen random number, and add it to the 'key' variable
  6. print the key variable
  7. Tell the user to remember this key to decrypt the message

I would also like to say, I know there is not a message to encrypt currently, I am aware of this. This is just practice that needs to be done. Here is my code:

import random

key = ''

for i in range(0,8):
    random_number = (random.randint(33,162))
    str(chr(random_number.append(key)))

print(key)
print("Remember this key to decrypt your message!")

when inside of the shell (after running, I get the following error...)

"str(chr(random_number.append(key)))

AttributeError: 'int' object has no attribute 'append'"

Upvotes: 0

Views: 35

Answers (3)

Josete Manu
Josete Manu

Reputation: 187

Try to do this:

import random

key = ''

for i in range(0,8):
    random_number = (random.randint(33,162))
    key += str(random_number) # main change

print(key)
print("Remember this key to decrypt your message!")

Upvotes: 0

R Nar
R Nar

Reputation: 5515

.append() method is for list and list-like objects, not for simple, immutable types like int,str,tuple etc. i think you wanted this:

key +=str(random_number)

instead to set key = key + str(random_number)

Upvotes: 1

ham_string
ham_string

Reputation: 139

You can only append to lists. You have an empty string to which you have to add. Now think which option we use with strings and you got it ;-)

Upvotes: 0

Related Questions