I am trying to make a small program that will jumble up the letters of the alphabet(In simple terms)
I have tried to use things like list.pop() or list.remove(), But those did nothing
import random
def rand_let():
i = 26
alphabet = str('')
for a in range(1, 26):
key = ['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']
print(len(key))
print(key)
letter = random.randint(1, i)
print(key[letter])
letters = key[letter]
alphabet += str(letters)
key.remove(letter)
i -= 1
rand_let()
I want it to jumble up the alphabet,
it is, but the way it is doing it will make letters repeat(I don't want it to repeat)
The shuffle function from random will save you many lines, and does what you're looking for:
import random
alphabet = ['A', 'B', 'C']
random.shuffle(alphabet)
print(alphabet)
#Ex: ['C', 'A', 'B']
The reason you are getting duplicates is that although you have code to remove the letter from the key list, the line that declares the key list is within the loop as well. Try moving the line
key = ['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']
above the for loop. Or, as suggested in other answers, use a library to do this for you.
import random
import string
alphabet = [letter for letter in string.ascii_uppercase]
random.shuffle(alphabet)
print(alphabet)
This will just shuffle the list and then print the elements of the list one by one for each iteration of the for loop.
import random
key = ['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']
random.shuffle(key)
def choice(x):
for letter in key:
print()
choice(key)
Related
I simply want to reprint what the user enters for -Wordinput-, what I have now works fine but is there an easier way in which I can do this? Please help!
list = ['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']
wordInput = input('Print your word: ')
for i in range(len(wordInput)):
indivLetter = wordInput[i].lower()
finalWord = [match for match in list if indivLetter in match]
print(*finalWord, end='')
This is what I have so far:
strVar = 'Together We Thrive, Class of 2025!'
def stringToList(string):
listRes = list(string.split(" "))
return listRes
strVar = 'Together We Thrive, Class of 2025!'
strVarList = print(stringToList(strVar))
I know I need to have append and sort somewhere in there.
These are the instructions:
Convert the provided string to a list using a for loop or built-in function and store the result in a new variable.
Sort the list.
Print the slice of the list that only includes letters.
This what my answer I supposed to look like:
['C', 'T', 'T', 'W', 'a', 'e', 'e', 'e', 'e', 'f', 'g', 'h', 'h', 'i', 'l', 'o', 'o', 'r', 'r', 's', 's', 't', 'v']
Kindly try:
sorted([x for x in 'Together We Thrive, Class of 2025!' if x.isalpha()])
Or, pass the name of the defined variable.
This outputs:
['C',
'T',
'T',
'W',
'a',
'e',
'e',
'e',
'e',
'f',
'g',
'h',
'h',
'i',
'l',
'o',
'o',
'r',
'r',
's',
's',
't',
'v']
please try this:
strvar = "Together we Thrive, Class of 2025!"
Listt=[ ]
for letter in strvar:
if (letter.isalpha()):
Listt.append(letter)
Listt.sort()
print(Listt)
I am new to programming so cut me some slack. I apologise in advance for asking such a dumb question and thank you for your time.
What I'm trying to do:
I want to make a program that converts letters with their position in the alphabet. I am finding the position of X character by searching it's index in a list (where I have put the alphabet) with a while loop. When all the characters are converted I presumably print the result in a string. But it doesn't have any output when I run it. It doesn't show any errors or warnings so I'm completely lost on what the problem might be.
Why is that and how do I resolve this issue?
Again, thanks for your patiance with me.
Here's the code:
def alphabet_position(text):
alphabet_pos = []
result = ""
index_1 = len(text)
i = 0
while i < index_1:
list_pos = index_1 - 1
character = text[list_pos]
if character.isdigit:
pass
else:
alphabet_low = ['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']
alphabet_cap = ['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']
if character in alphabet_low is True:
alphabet_pos.append(alphabet_low.index(character))
elif character in alphabet_cap is True:
alphabet_pos.append(alphabet_cap.index(character))
else:
pass
i += 1
for char in alphabet_pos:
result = str(alphabet_pos[char]) + " "
char += 1
print(result)
alphabet_position("hello")
Well, there are many things to unpack in your code.
First of all, if character.isdigit: should be replaced with if character.isdigit(): . if character.isdigit: basically asks whether isdigit function is defined and will always return True.
Next, you don't need character in alphabet_low is True, character in alphabet_low is enough.
Next, you don't really use i anywhere and character variable would still be the same letter. Also, you don't need while loop, you can just iterate over the letters of the text.
Next, I don't really understand what you want to do in the last 3 lines before print(result) so I just rewrote it the way I would do it.
I would rewrite your code like this:
def alphabet_position(text):
alphabet_pos = []
result = ""
alphabet_low = ['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']
alphabet_cap = ['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']
for character in text:
if not character.isdigit():
if character in alphabet_low:
alphabet_pos.append(alphabet_low.index(character))
elif character in alphabet_cap:
alphabet_pos.append(alphabet_cap.index(character))
for char in alphabet_pos:
result += str(char) + " "
print('result:', result)
I suggest when you write code, use print statements more often to see how what you've written so far works.
You could also solve it like this:
ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
dct = {y: x for x, y in enumerate(ascii_lowercase)}
def alphabet_position(text):
return [dct[char.lower()] for char in text if char != ' ']
alphabet_position('some text')
# Out[61]: [18, 14, 12, 4, 19, 4, 23, 19]
I'm trying to create a program that opens a random Bitly link in your browser.
import random
import webbrowser
#set the length of the url
length = random.randint(1,7)
#list of all possible characters in the key
characters = ['a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F', 'g', 'G', 'h',
'H', 'i', 'I', 'j', 'J', 'k', 'K', 'l', 'L', 'm', 'M', 'n', 'N', 'o', 'O', 'p', 'P',
'q', 'Q', 'r', 'R', 's', 'S', 't', 'T', 'u', 'U', 'v', 'V', 'w', 'W', 'x', 'X', 'y',
'Y', 'z', 'Z',]
#create the string of text on the end of the link
key = []
for x in range (1, length):
key.append(random.choice(characters))
webbrowser.open_new_tab('https://bit.ly/', *key, sep='')
When I do this it gives me
TypeError: open_new_tab() got an unexpected keyword argument 'sep'
But if I can't put "sep" into that argument, how else am I suposed to make the link open correctly?
You can use
key_str = "".join(key)
to join all of the characters in key into a single string. The URL could be constructed with
url = f"https://bit.ly/{key_str}"
By the way, instead of defining a list of upper- and lower-case letters, you can use string.ascii_letters.
i have tried but can't seem to find my mistake in my code.
My code is suppose to switch all the alphabetic characters (like a/aa/A/AA) and do nothing with the rest but when i run the code it doesn't give an error yet do what i want.
Could anyone tell me what i have done wrong or have forgotten?
letter = input("type something")
shift = int(input("type how many shifts"))
if letter in ['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', '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']:
a = ord(letter) + shift
b = chr(a)
print(b)
else:
print(letter)
EDIT: thanks for the == replacement for in! Does someone know why using more than one character in letter gives the same print?(Desired output: when i put in abc and 1 i want it to print bcd)
I suppose you want to shift the letters so if the input letter is 'a' and shift is 3, then the output should be 'd'.
In that case replace
if letter == ['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', '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']:
with
if letter in ['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', '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']:
Or better yet as Tempux suggested you can use
if letter.isalpha()
If you want to shift multple letters you need to loop across each character. Try the following code for multiple letters
letter = input("type something")
shift = int(input("type how many shifts"))
s = ""
for l in letter:
if l.isalpha():
a = ord(l) + shift
s += chr(a)
else:
s += l
print(s)
You compare letter with list, but i think you want to check for contain letter in list, so you should just replace == to in
From the looks of it, I'd say you're more after something like this:
import string
text = input("type something> ")
shift = int(input("enter number of shifts> "))
for letter in text:
index = ord(letter) - ord('a') + shift
print(string.ascii_letters[index % len(string.ascii_letters)])