0

I'm trying to create a variable that's a string from a list The list is just the alphabet (Don't ask why, it just has to for the program)

passwords = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]

Basically, I used to have a variable that took 4 random letters from it to create one string that looked like this:

correctPassword = random.sample(passwords,4)

What I need is 4 specific characters from the list to create the same kind of string variable, the word "STOP".

An answer to this would be greatly appreciated, I'm trying to recreate the hacking minigame from Fallout, if any of you know what that is, thanks!

5
  • 2
    Please show what you have tried so far to solve this. Explain using your code what issues you are facing. Commented Jun 7, 2017 at 0:56
  • 2
    Can you try and word it a little clearer? Commented Jun 7, 2017 at 0:57
  • Since the letters are in order, cant you just use an index for the indexes of s,t,o,p? Commented Jun 7, 2017 at 0:58
  • please tell me you didn't type out passwords and did passwords = list(string.ascii_uppercase) Commented Jun 7, 2017 at 1:05
  • 1
    he is not using passwords for security purposes, i think he is using this as a check to see if a player enters predetermined password. Commented Jun 7, 2017 at 1:09

3 Answers 3

1

I'm not sure I understand what you're trying to do, but... To create a string variable equal to "STOP":

x = "STOP"

The line of code

correctPassword = random.sample(passwords,4)

doesn't create a string variable but a list of strings 4 items long, each item being a 1-character string. Using x as defined above, the expression x == correctPassword will always be false.

To consolidate those into a single string:

correctPasswordAsString = ''.join(correctPassword)

A comparison x == correctPasswordAsString will be true if correctPasswordAsString is "STOP", false otherwise.

Sign up to request clarification or add additional context in comments.

Comments

0

You can just create the variable as the following:

correctPassword = ["S", "T", "O", "P"]

Or, you can use the indexes of the letters:

correctPassword = [passwords[18], passwords[19], passwords[14], passwords[15]]

Comments

0

You can use this for create a string variable from list:

import random
passwords = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
correctPassword = random.sample(passwords,4)
result = ""
result = result.join(correctPassword)
print(result)

and get something like:STOP

The Python join() method is a string method. It merges a list of objects into a string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.