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.
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 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)
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)])
My script is taking the first argument as shown in the input below and I am trying to create a list out of it but incorrectly as shown in output, can anyone provide inputs on how to fix this?
projects = sys.argv[1]
ProjectList = list(projects)
INPUT:-
python script.py platform/system/bt,platform/packages/apps/Bluetooth,platform/vendor/qcom-proprietary/ship/bt/hci_qcomm_init
output:
['p', 'l', 'a', 't', 'f', 'o', 'r', 'm', '/', 's', 'y', 's', 't', 'e', 'm', '/', 'b', 't', ',', 'p', 'l', 'a', 't', 'f', 'o', 'r', 'm', '/', 'p', 'a', 'c', 'k', 'a', 'g', 'e', 's', '/', 'a', 'p', 'p', 's', '/', 'B', 'l', 'u', 'e', 't', 'o', 'o', 't', 'h', ',', 'p', 'l', 'a', 't', 'f', 'o', 'r', 'm', '/', 'v', 'e', 'n', 'd', 'o', 'r', '/', 'q', 'c', 'o', 'm', '-', 'p', 'r', 'o', 'p', 'r', 'i', 'e', 't', 'a', 'r', 'y', '/', 's', 'h', 'i', 'p', '/', 'b', 't', '/', 'h', 'c', 'i', '_', 'q', 'c', 'o', 'm', 'm', '_', 'i', 'n', 'i', 't']
You are looking to use split here. And specify that you want to split on a comma:
ProjectList = projects.split(',')
Output:
['platform/system/bt', 'platform/packages/apps/Bluetooth', 'platform/vendor/qcom-proprietary/ship/bt/hci_qcomm_init']
dont use list. it's better if you got with split()
ProjectList = projects.split(',')
You can split the string by using a delimiter see below:
projects = sys.argv[1]
ProjectList = projects.split(",")
You can call split method on project and assign it to a new variable.
ProjectList = project.split(',')
Python has a built-in split method that takes a string and splits it into a list, splitting into new elements at specific delimiters. All you have to do is:
ProjectsList = projects.split(",")
You specify the delimiter within the parenthesis when you call split, in this case, a comma.