Creating a function that creates two lists in Python 3 - python-3.x

I am trying to create a function, getStocks, that gets from the user two lists, one containing the list of stock names and the second containing the list of stock prices. This should be done in a loop such that it keeps on getting a stock name and price until the user enters the string 'done' as a stock name. The function should return both lists. My main issues are figuring out what my parameters are, how to continuously take in the name and price, and what type of loop I should be using. I am very new to programming so any help would be appreciated. I believe I'm close but I am unsure where my errors are.
def getStocks(name,price):
stockNames = []
stockPrices = []
i = 0
name = str(input("What is the name of the stock?"))
price = int(input("what is the price of that stock?"))
while i < len(stockNames):
stockNames.append(name)
stockPrices.append(price)
i += 1
else:
if name = done
return stockNames
return stockPrices

Your question is a bit unclear but some things off the bat, you cant have two return lines, once you hit the first, it leaves the function. Instead you'do write something like
return (stockNames, stockPrices)
Secondly while loops dont have an else, so you'd actually set up your while loop, then setup an if statement at the beginning to check if the string is 'done', then act accordingly. Break will get you out of your last while loop, even though it looks like it's associated with the if. So something like this:
while i < len(stockNames):
if name.upper() == 'DONE':
break
else:
stockNames.append(name)
stockPrices.append(price)
i += 1
Also you have to use == (comparison) instead of = (assignment) when you check your name = done. And dont forget done is a string, so it needs to be in quotations, and I used .upper() to make the input all caps to cover if its lower case or uppercase.
If you can clear up your question a little bit, I can update this answer to include everything put together. I'm not quite understanding why you want to input a list and then also take user input, unless you're appending to that list, at which point you'd want to put the whole thing in a while loop maybe.
Update:
Based on your comment, you could do something like this and enclose the whole thing in a while loop. This takes the incoming two lists (assuming you made a master list somewhere) and sends them both into the getStocks function, where someone can keep appending to the pre-existing list, and then when they type done or DONE or DoNe (doesn't matter since you use .upper() to make the input capitalized) you break out of your while loop and return the updated lists:
def getStocks(name, price):
stockNames = name
stockPrices = price
while 1:
inputName = str(input("What is the name of the stock?"))
inputPrice = int(input("what is the price of that stock?"))
if name.upper() != 'DONE':
stockNames.append(inputName)
stockPrices.append(inputPrice)
else:
break
return (stockNames, stockPrices)
But really, depending on the rest of the structure, you might want to make a dictionary instead of having 2 separate lists, that way everything stays in key:value pairs, so instead of having to check index 0 on both and hoping they didn't get shifted by some rogue function, you'd have the key:value pair of "stock_x":48 always together.

Related

append() not working corretly in IF statement

Im only about a month into python so still learning and I cant see a similar question or more like problem. I have a list and dictionary that I want to add new entries to. When I run this code:
n = input('Enter variable name to check: ')
varDict = {} #Key = label and Value = description
valList = [] #Lists the referenced values from the code
if n in varDict:
print(varDict[n])
print(valList[n])
if n not in varDict:
new = input('Not found, make new entry?')
if new == 'yes':
desc = input('Enter description: ')
varDict[n] = desc
valList.append(n)
print(n, 'has been added to the list')
elif new == 'no':
print('Done')
it seems to work fine, but when you re-run the code and enter new values, it overrides the first entry in the list and dictionary instead of adding new entries in both. Ive also googled this and my code seems 100% fine, but it just keeps the list and dict's length at 1 entry.
Im not sure what the problem is and any help would be great, thanks.
EDIT: I added the full code.
When you run a python program whatever data you put in is temperary, as it only is saved to variables while you're running it.
So when you rerun this program, and aren't seeing what you last entered in, that's purely python's functionality, as it is with every other (scripting) programming language.
If you want to keep these values, think about adding them to a file (learn open()) and retrieving them at the start of the program and putting them into the variables (varDict, valList) before asking for user input.

How to define a function that will convert list into dictionary while also inserting 'None' where there is no value for a key (Python3)?

Say we have a contact_list like this:
[["Joey", 30080],["Miranda"],["Lisa", 30081]]
So essentially "Miranda" doesn't have a zipcode, but with the function I want to define, I'd like it to automatically detect that and add "None" into her value slot, like this:
{
"Joey": 30080,
"Miranda": None,
"Lisa": 30081
}
So far I have this, which just converts the list to a dict:
def user_contacts(contact_list):
dict_contact = dict(contact_list)
print(dict_contact)
Not sure where I go from here, as far as writing in the code to add the None for "Miranda". Currently, I just get an error that says the 1st element ("Miranda") requires two lengths instead of one.
Eventually I want to just a pass any list like the one above in the defined function: user_contacts and, again, be able to get the dictionary above as the output.
user_contacts([["Joey", 30080],["Miranda"],["Lisa", 30081]])
so here is what you can do. you can check to see if the len of a certain element in your list, meets the expectations (in this case, 2 for name and zipcode). then if it fails the expectations, you can add "none":
contacts = [["Joey", 30080], ["Miranda"], ["Lisa", 30081]]
contacts_dict = {}
for c in contacts:
if len(c) < 2:
c.append('None')
contacts_dict.update({c[0]: c[1]})
print(contacts_dict)
and the output is:
{'Joey': 30080, 'Miranda': 'None', 'Lisa': 30081}
Try this:
def user_contacts(contact_list):
dict_contact = dict((ele[0], ele[1] if len(ele) > 1 else None) for ele in
contact_list)
print(dict_contact)

Defining a function to find the unique palindromes in a given string

I'm kinda new to python.I'm trying to define a function when asked would give an output of only unique words which are palindromes in a string.
I used casefold() to make it case-insensitive and set() to print only uniques.
Here's my code:
def uniquePalindromes(string):
x=string.split()
for i in x:
k=[]
rev= ''.join(reversed(i))
if i.casefold() == rev.casefold():
k.append(i.casefold())
print(set(k))
else:
return
I've tried to run this line
print( uniquePalindromes('Hanah asked Sarah but Sarah refused') )
The expected output should be ['hanah','sarah'] but its returning only {'hanah'} as the output. Please help.
Your logic is sound, and your function is mostly doing what you want it to. Part of the issue is how you're returning things - all you're doing is printing the set of each individual word. For example, when I take your existing code and do this:
>>> print(uniquePalindromes('Hannah Hannah Alomomola Girafarig Yes Nah, Chansey Goldeen Need log'))
{'hannah'}
{'alomomola'}
{'girafarig'}
None
hannah, alomomola, and girafarig are the palindromes I would expect to see, but they're not given in the format I expect. For one, they're being printed, instead of returned, and for two, that's happening one-by-one.
And the function is returning None, and you're trying to print that. This is not what we want.
Here's a fixed version of your function:
def uniquePalindromes(string):
x=string.split()
k = [] # note how we put it *outside* the loop, so it persists across each iteration without being reset
for i in x:
rev= ''.join(reversed(i))
if i.casefold() == rev.casefold():
k.append(i.casefold())
# the print statement isn't what we want
# no need for an else statement - the loop will continue anyway
# now, once all elements have been visited, return the set of unique elements from k
return set(k)
now it returns roughly what you'd expect - a single set with multiple words, instead of printing multiple sets with one word each. Then, we can print that set.
>>> print(uniquePalindromes("Hannah asked Sarah but Sarah refused"))
{'hannah'}
>>> print(uniquePalindromes("Hannah and her friend Anna caught a Girafarig and named it hannaH"))
{'anna', 'hannah', 'girafarig', 'a'}
they are not gonna like me on here if I give you some tips. But try to divide the amount of characters (that aren't whitespace) into 2. If the amount on each side is not equivalent then you must be dealing with an odd amount of letters. That means that you should be able to traverse the palindrome going downwards from the middle and upwards from the middle, comparing those letters together and using the middle point as a "jump off" point. Hope this helps

How can I transform the string of characters back into words?

I've been trying to learn Python for the past two months or so, but I'm really only now getting my hands dirty with it, so I thank you in advance for your patience and insight.
I was working on a project where I was cleaning the names in a dataset. That means filtering out the names of the apps who have foreign characters (that is to say, ord(character) > 127.
However, it turns out that this approach removed too many legitimate apps since the emojis in those were coming back as out of that range.
The workaround is to allow up to one foreign character. So it's pretty straightforward for that part; I can simply scan the characters of the names in each list. The part I'm having trouble with is telling Python where in the loop to add a name to the "cleaned" list (the final version of app names having <=1 one error. (The requirements are actually different in my project, but I'm trying to keep it as simple as possible in this example.)
To simplify the problem a bit, I was working on a dummy list. I have included that for you.
Where do I add the code so that after that final iteration of each name, the name is added to the list entitled cleanedNameList to only append names with <=1 foreign character?
When I've tried appending a 'clean' name to the list before (a name that had <=1 foreign characters in it), it also sometimes adds the ones with more than three foreign characters. I think this is due in part to me not knowing where to put the exception counter.
nameList = ['うErick', 'とうきhine', 'Charliと']
cleanedNameList = []
exceptions = 0
for name in nameList:
print('New name', name, 'being evaluated!')
exceptions = 0
for char in name:
print(char, 'being evaluated')
ascii_value = ord(char)
if ascii_value < 127:
continue
elif ascii_value > 127:
exceptions+=1
print(exceptions, 'exception(s) added for', name)
#where would I add append.cleanedNamesList(name) ?
So, TL;DR: how do I scan a list of names, and once done scanning the list, add those names to a new list only IF they have <=1 foreign character.
def canAllow(s):
return sum((1 for char in s if ord(char)>127), 0) <= 1
cleanList = [name for name in nameList if canAllow(name)]

Entering a word and then searching through and array of words to find the word

I am trying to create a program which checks to see if words entered (when run) are in an array. I would like to use a loop for this.
I have created a list of words and tried a for loop however the code is proving to be erroneous.
def Mylist():
Mylist= [Toyota,BMW,Pontiac,Cadillac,Ford,Opel]
Search=input("Enter a word")
Mylist[1]="Toyota"
for loop in range (1,6):
if Mylist[loop]==Search:
print("found")
break
I have repeated line 4 for the other car manufacturers.
TypeError: 'function' object does not support item assignment
First, here some recommendations to start:
Indentation in Python is IMPORTANT, be careful to have the right indentation. You must take special care when posting code here in SO so your code does not look like complete gibberish.
You should read Naming conventions. TL;DR we use snake_case for naming functions and variables.
If you are not using an IDE (such as PyCharm) to program, or something intelligent that shows the information on functions, you should always check out the documentation (it is beautiful).
Check out the difference between "Toyota" and Toyota. The first one has quotes, it is a string (i.e. chain of characters), it is a primitive type such as integer and boolean; the second is a token that is to be evaluated, it has to be defined before, such as variables, functions and classes.
Search in the docs if there is an in-built function that already does the job you want.
Check out return values in functions. Functions evaluate to None when you do not explicit a return value.
Now to your question. As some people pointed out, there is the in keyword that does exactly what you want.
CAR_BRANDS= ["Toyota", "BMW", "Pontiac", "Cadillac", "Ford","Opel"]
def check_car():
word = input("Enter a word: ")
if word in CAR_BRANDS:
print("found")
return True
print("not found")
return False
If you don't care about the print you can just do return word in CAR_BRANDS
If you actually want to challenge yourself to write the logic, you were right in choosing a for-loop to iterate over the list.
Index in Python starts from 0, and that range does not give you all the indexes to iterate over your list, you are missing the 0 index. Also, we don't like magic numbers, instead of hard-coding the length of your list of car brands, better compute the length!
for i in range(len(CAR_BRANDS)):
if CAR_BRANDS[i] == word:
print("found")
But even better you can directly iterate over the items in your list, no need to do the range, which will give you something like:
CAR_BRANDS= ["Toyota", "BMW", "Pontiac", "Cadillac", "Ford","Opel"]
def check_car():
word = input("Enter a word: ")
for brand in CAR_BRANDS:
if brand == word:
print("found")
return True
print("not found")
return False
If you have any more questions, do not hesitate! Happy coding.

Resources