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.
Related
Thanks in advance for your assistance.
I have two dictionaries: parent_dict has a series of sets as values & pub_plat_dict is a look-up dictionary that seeks to correct the names of the items that make up the sets in parent_dict.
The function update_dict allows names where it finds a '.' to pass. If it doesn't find a name then it will try to find the name in the pub_plat_dict. If found it will .remove the old name and .add the updated name. If the name isn't present, then I want the program to move to the next item.
When I run the function update_dict corrects the first item in the parent_dict, accurately skips multiple items that don't need to be updated but then doesn't .remove or .add the other wrongly named items.
Sample Data
parent_dict = {
'49d238407e0102ba':{'opportunity_history'}
, 'f9d53c74ec1d2ff6':{'servicer.trial_balance','src_platform.loan','src_platform.loan_disbursement'}
, 'fc35a98e0cfaab3d':{'loan', 'loan_agreement', 'opportunity_compliance_flag','zodiac'}
}
pub_plat_dict = {'loan':'src_platform.loan',
'opportunity_compliance_flag':'src_platform.opportunity_compliance_flag',
'opportunity_history':'src_platform.opportunity_history',
'loan_agreement': 'src_platform_mosaic_live.loan_agreement'}
Function
def update_dict(parent_dict):
for tbls in parent_dict.values():
for tbl in tbls:
if tbl.find(".") != -1:
pass
else:
try:
update = pub_plat_dict[tbl]
tbls.remove(tbl)
tbls.add(update)
except:
pass
return(parent_dict)
Output
{'49d238407e0102ba': {'src_platform.opportunity_history'}, 'f9d53c74ec1d2ff6': {'src_platform.loan', 'src_platform.loan_disbursement', 'servicer.trial_balance'}, 'fc35a98e0cfaab3d': {'opportunity_compliance_flag', 'loan_agreement', 'loan', 'zodiac'}}
NOTE: the first item is updated correctly but everything else is left unchanged.
I did the following loop to try to figure out my error (keeping it as close to the update_dict code as I could).
for tbls in parent_dict.values():
for tbl in tbls:
if tbl.find('.') != -1:
print("UNCHANGED-" + tbl)
else:
try:
print("CHANGED-" + pub_plat_dict[tbl])
except:
print("FAILURE-"+ tbl)
It gives me the following output:
UNCHANGED-src_platform.opportunity_history
UNCHANGED-src_platform.loan
UNCHANGED-src_platform.loan_disbursement
UNCHANGED-servicer.trial_balance
CHANGED-src_platform.opportunity_compliance_flag
CHANGED-src_platform_mosaic_live.loan_agreement
CHANGED-src_platform.loan
FAILURE-zodiac
Aside from the capitalized- word this is what I would expect the parent_dict would now look like. So my .remove and .add aren't working consistently.
EDIT: I also substituted .discard for .remove, the output did not change.
Any assistance would be greatly appreciated.
I couldn't get the function to work as I created it. Part of the issue is where the return statement appears. Using a return inside of a loop will break it and exit the function even if the iteration is still not finished. Another issue might be the redundant logic in the function.
I decided to create a new dictionary and use a list as the dict's values instead of sets. I simplified the logic in the function and got rid of the if statement which seemed redundant in light of the try clause.
from collections import defaultdict
cln_parent_dict = defaultdict(list)
def update_dict(parent_dict):
for key in parent_dict:
for value in parent_dict[key]:
try:
cln_parent_dict[key].append(pub_plat_dict[value])
except:
cln_parent_dict[key].append(value)
return(cln_parent_dict)
when I run the function I get what I expect:
Function Output
defaultdict(<class 'list'>, {'49d238407e0102ba': ['src_platform.opportunity_history'], 'f9d53c74ec1d2ff6': ['servicer.trial_balance', 'src_platform.loan_disbursement', 'src_platform.loan'], 'fc35a98e0cfaab3d': ['zodiac', 'src_platform_mosaic_live.loan_agreement', 'src_platform.opportunity_compliance_flag', 'src_platform.loan']})
Overall the change seems to work for the 100K items in the dataset.
Thanks to everyone for taking a look.
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)]
I'm working on a quiz program and need some help. I'm trying to replace words one at a time, but Python isn't saving the previously replaced string. Here is a mini example of what I mean:
replacedQuiz=""
easyQuiz = """
You can change a string variable to an integer by typing (__1__)
in front of the variable. It also works vice versa, you can change an
integer
variable to a string by typing (__2__). This is important to remember before
you __3__ strings together, or else a TypeError will occur. While adding an
integer to a string, it is important to separate it using a __4__ (use the
symbol). \n"""
def replaceWord(replaced, quiz, numCount):
if numCount == 1:
replaced = quiz.replace("__1__", "int")
if numCount == 2:
replaced = replaced.replace("__2__", "str")
if numCount == 3:
replaced= replaced.replace("__3__", "concatenate")
if numCount == 4:
replaced= replaced.replace("__4__", ",")
print replaced
def easy():
QCount=1
print easyQuiz
while QCount < 5:
replaceWord(replacedQuiz, easyQuiz, QCount)
QCount += 1
print easy()
I thought that by making a String called replacedQuiz, it would save the first replacement and then I could continue replacing the words inside the quiz and updating it. Please help! I don't know where I'm going wrong
You seem to have made a slight mistake in the scope of your variable replacedQuiz (it'd certainly suggest that you check out some explanation of this topic). Basically, you are replacing replacedQuiz by its new value only within your current function. Your other functions only have access to the global value you defined earlier. There are several ways to fix this (e.g. the global keyword) but the standard way would be to return the new replacedQuiz from your function.
To do so, add the following line to the end of your replaceWord function:
return replacedQuiz
This tells Python to use this value at the line it was called at. You can then define a new value for replacedQuiz within easy by just defining it as the returned value:
replacedQuiz = replaceWord(replacedQuiz, easyQuiz, QCount)
Good afternoon! I am relatively new to Python - and am working on an assignment for a class.
The goal of this code is to download a file, add a line of data to the file, then create a while loop that iterates through each line of data, and prints out the city name and the highest average temp from the data for that city.
My code is below - I have the output working, no problem. The only issue I am running into is an IndexError: list index out of range - at the end.
I have searched on StackOverflow - as well as digging into the range() function documentation online with Python. I think I just need to figure to the range() properly, and I'd be done with it.
If I take out the range, I get the same error - so I tried to change the for/in to - for city in mean_temps:
The result of that was that the output only showed 4 of the 7 cities - skipping every other city.
Any advice would be greatly appreciated!
here is my code - the screenshot link below shows output and the error as well:
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/world_temp_mean.csv -o mean_temp.txt
mean_temps = open('mean_temp.txt', 'a+')
mean_temps.write("Rio de Janeiro,Brazil,30.0,18.0")
mean_temps.seek(0)
headings = mean_temps.readline().split(',')
print(headings)
while mean_temps:
range(len(city_temp))
for city in mean_temps:
city_temp = mean_temps.readline().split(',')
print(headings[0].capitalize(),"of", city_temp[0],headings[2], "is", city_temp[2], "Celsius")
mean_temps.close()
You have used a while loop, when you actually want to use a for loop. You have no condition on your while loop, therefore, it will evaluate to True, and run forever. You should use a for loop in the pattern
for x in x:
do stuff
In your case, you will want to use
for x in range(len(city_temp)):
for city in means_temp:
EDIT:
If you have to use a while loop, you could have variable, x, that is incremented by the while loop. The while loop could run while x is less than range(len(city_temp)).
A basic example is
text = "hi"
counter = 0
while counter < 10:
print(text)
counter += 1
EDIT 2:
You also said that they expected you to get out of a while loop. If you want a while loop to run forever unless a condition is met later, you can use the break command to stop a while or for loop.
I've been stuck with this as well with the index error. My original code was:
city_temp = mean_temp.readline().strip(" \n").split(",")
while city_temp:
print("City of",city_temp[0],headings[2],city_temp[2],"Celcius")
city_temp = mean_temp.readline().split(",")
So I read the line then, in the loop, print the line, create the list from reading the line and if the list is empty, or false, break. Problem is I was getting the same error as yourself and this is because city_temp is still true after reading the last line. If you add..
print(city_temp)
to your code you will see that city_temp returns as "" and even though it's an empty string the list has content so will return true. My best guess (and it is a guess) it looks for the split condition and returns back nothing which then populates the list as an empty string.
The solution I found was to readline into a string first (or at the end of the whole loop) before creating the list:
city_temp = mean_temp.readline()
while city_temp:
city_temp = city_temp.split(',')
print(headings[0].capitalize(),"of",city_temp[0],headings[2],"is",city_temp[2],"Celcius")
city_temp = mean_temp.readline()
This time city_temp is checked by the while loop as a string and now returns false. Hope this helps from someone else who struggled with this
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.