My python list doesn't understand letters :( [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
This post was edited and submitted for review 3 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
my Code doesent understand letters in the list i would like somone to help me fix this
usernames = (BTP, btp, Btp, BTp)
def username(usernames2):
if usernames == input('whats your username? : ')
Its a simple username system, i plan to use for a interface im making.

usernames is defined as a tuple of 4 items, with the names BTP, btp, Btp, and BTp. You said "list" in your title but your code has no actual lists. Lists use brackets, tuples use parentheses.
Anyway, I'm assuming you actually want to check if the user's input actually was equal to the letters "btp" and you want the check to be case-insensitive, hence why you included all combos of uppercase and lowercase.
The main issue is that you didn't put quotes around the strings, so you have just 4 bare names sitting in your code which the interpreter expects to have been defined previously. But, you actually don't have to define all the possible combinations of uppercase and lowercase in the first place - there's a much easier method to do a case-insensitive string compare, here.
So, your code just needs to look like:
usename = "btp"
def username(usernames2):
if input('whats your username? : ').lower() == username
Or, if you want to check against multiple usernames, you can use the in operator:
usenames = ["btp", "abc", "foo", "bar"]
def username(usernames2):
if input('whats your username? : ').lower() in usernames

If you haven't declared BTP, btp, Btp, and BTp you will get a NameError
If you wanted to use strings you need single or double quotation marks:
usernames = ("BTP", "btp", "Btp", "BTp")
With that you create a tuple containing four string elements.
The next issue is with your if condition as you compare if a tuple is equal a string.
Try storing the input given from the user in a variable:
def username(usernames):
user_input = input('whats your username?: ')
if user_input in usernames:
# Do something when username is found

Related

Looking for Words in a List with Similar Letters [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a list with types of cheese and I want to be able to search for gouda by just writing "g" and "o" instead of writing the full sentence.
I've looked for solutions but none are exactly what I am looking for. Maybe this is something common but I just started a week ago with Python I don't know many of the terms.
For some reason I got this cancelled so Im writing this paragraph so the person that answered can answer again
Here is a link to another StackOverflow post I found: Link.
This explains what I think you are looking for in your problem.
This code will print gouda from the wordlist:
wordlist = ['gouda','miss','lake','que','mess']
letters = set('g')
for word in wordlist:
if letters & set(word):
print(word)
All you have to do is set whatever letters you want to search for in the list to the letter variable (in the brackets) and it will return the words that contain the letters you entered.
ex. I added gouda (your example) to this list. If you set the letters variable, to g, it searches the wordlist for any words that contain the letter g, in this case it will return gouda from the wordlist as it is the only word that contains the letter 'g'.
The only downfall of this is if you enter 'ms' to search this wordlist you will get two responses, miss and mess as they both contain letters 'm,s' so in some cases you will have to be more specific if you only want one word to be returned.
Note: this is not my code, I got it from the post linked here, and above.

getting unhashable type: 'list' while using .split() [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am using python 3 to begin with. I am trying to split the input from the console to match a key in my dictionary and the display the value of said key on the console. I've been trying things for hours and have decided to break down and ask for help. Here's some of the code.
enter = input("test ").split()
names1 = {8410:"A", 8422:"B", 8450:"C", 8386:"D", 8394:"E", 8395:"F", 8318:"G", 8451:"H", 8348:"I", 8294:"J", 8349:"K"}
if enter in names1:
print(names1[enter])
I have 16 dictionaries with 7000+ names in them with employee ids. My main goal here is to be able to type in a URL that has an ID in it, ex: www.domain.com/8450 and have the console only grab the 8450 and then display C.
Thanks in advance.
If you know your URLs are going to be like that use rsplit('/') like so:
>>> enter = int(input('test: ').rsplit('/')[-1])
test: www.domain.com/8450
>>> enter
8450
>>> names1[enter]
'C'
Also your keys are stored as ints so convert the input to int using int().
split() returns a list
Eg:
>>> input('test:').split()
test:hello
>>> ['hello']
You are checking if any of the keys in the dict are a list, like this:
>>> if ['input'] in {'input': 'sample data'}
Which will not work.
You need to sanitize your inputs. There's many ways to do this. One example might be:
>>> if isinstance(enter, list):
>>> enter = enter[0]
The choice is yours.

Python Username Generator [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
The following is my code:
string1 = (input("What is your name?")) #creates a string, stores the name in it
first1 = string1[:1]
string2 = (input("What is your last name?"))
first3 = string2[:3]
from random import randint
sentence = "".join((str(randint(0,9)), first1.lower(), first3.upper()))
print (sentence)
sentence = "".join((str(randint(0,9)), first1.lower(), first3.upper()))
print (sentence)
It works, but I am having some trouble. I need to loop this 5 times - but it doesn't work for some reason!
P.S. Python 3!
You are creating a tuple called sentence, rather than a string
If you change that line to this:
sentence = "".join((str(randint(0,9)), first1.lower(), first3.upper()))
It will create a string that has no gaps, like so when printed:
What is your name?First
What is your last name?Last
5fLAS
You are creating a list, not a string so it seems logical that you get issues when trying to print it...
To append to a string you can do that :
sentence = str(randint(0,9))+(first1.lower())+(first3.upper())
In Python, you don't give a type to your variables, it depends of what you put INTO them.
In Python, elements between parenthesis "()" are considered as TUPLE (a type similar to a list, but that cant be modified), "[]" stands for a list, "{}" stands for a dictionnary. So you must NOT put parts of a string between parenthesis and separated with commas or you will turn them into a list of words. Instead you can use "+" that means to Python that you are making a string concatenation. Also note that i casted your random number into string. If you give 3 strings separated by "+" Python will automatically detect sentence as a String. Wp you've created a string !
If you have a list and want to get rid of the spaces you can also use :
"".join(yourlist)
However i don't recommend it here, you would be creating a list and using a join for nothing since you can create a string from the begining.
Edit: As for the loop, we can't answer if you don't copy paste the code of the loop and the error, it could be a million things.

How to make a the user input a command following a certain form in python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Sorry for the gory title, I can't work out what this would be called.
Basically, I want to ask the user "What would you like to do?" and have them be able to execute a number of commands. For example, they would say "Search for fox" and the program would do
if any(search in i for i in list):
print(list[search])
else:
print(search + " not found.")
So basically, how can I make the code detect the format "Search for [x]" and assign x to the variable search. Also, if there is a name for it, what would this be called? I think that would help me search for it next time.
The code below finds the keyword by removing the command (here search for) from the input given by the user, that way the search query is seperated.
def search(search_word):
# your search function here
pass
# get input from the user
user_input = raw_input("What would you like to do? ").lower()
# make the code easier to reuse
search_command = "search for"
if len(user_input) > 0:
if user_input.startswith(search_command):
# use slices to remove the first part of the input, and then remove all whitepsace
search_word = user_input[len(search_command):].strip()
search(search_word)

pseudo code in python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am fairly new to python
One of the exercises I have been given is to create the python pseudo code for the following problem:
write an algorithm that given a dictionary (list) of words, finds up to 10 anagrams of a given word.
I'm stuck on ideas on how to solve this.
Currently I have (it's not even proper pseudo)
# Go through the words in the list
# put each letter in some sort of array
# Find words with the letters from this array
I guess this is way too generalistic, I have searched online for specific functions I could use but have not found any.
Any help on specific functions that would help, in making slightly more specified pseudo code?
Here is some help, without writing the code for you
#define a key method
#determine the key using each set of letters, such as the letters of a word in
#alphabetical order
#keyof("word") returns "dorw"
#keyof("dad") returns "add"
#keyof("add") returns "add"
#ingest the word set method
#put the word set into a dictionary which maps
#key->list of up to 10 angrams
#get angrams method
#accept a word as a parameter
#convert the word to its key
#look up the key in the dictionary
#return the up to 10 angrams
#test case: add "dad" and "add" to the word set.
# getting angrams for "dad" should return "dad" and "add"
#test case: add "palm" and "lamp" to the word set.
# getting angrams for "palm" should return "palm" and "lamp"
#consider storing 11 angrams in the list
#a01, a02, a03, a04, a05, a06, a07, a08, a09, a10, a11.
#Then if a01 is provided, you can return a02-a11, which is 10 angrams

Categories

Resources