So I have a dictionary with these values:
{'AFG': (13, 0, 0, 2), 'ALG': (15, 5, 2, 8), 'ARG': (40, 18, 24, 28)}
And suppose a user wants to find out what the tuple is for the three letter term. Well, he or she would punch in say, 'AFG', and it would output.
[AFG, (13,0,0,2)]
However, it is coming up with the no match found code.
What is going on? I'm punching in the name exactly as it is in the dictionary, and there are no spaces or other white space characters that it is looking for either.
My code:
def findMedals(countryDict, medalDict):
answer = [['Code','country','Gold','Silver','Bronze']]
search_str = input('What is the country you want information on? ')
#this block of code gets the country's name and three letter code onto the list
for code, country in countryDict.items():
if code == search_str:
#This block should be getting executed if I type in, say, AFG
answer = [['country','code'], [country,code]]
print (answer)
else:
#but this is being ran instead
answer = [['country','code'], ['INVALID CODE', 'n/a']]
#return answer
print (medalDict) #debug code
#this block goes and appends the medal count to the list
for code, medalCount in medalDict.items():
if search_str in code:
#this block should be getting executed
answer.append([medalCount])
else:
#it will still put in the AFG's medal count, but with no match founds.
answer.append(['No Match Found'])
print (answer) #debug code
I think it might have something to do with the else statement in the for loop, but bringing it out of the loop doesn't seem to help either.
You are looping over all the keys and values in the dictionary. This means that the else block is going to be called for all keys in that loop that do not match:
for code, country in countryDict.items():
if code == search_str:
#This block should be getting executed if I type in, say, AFG
answer = [['country','code'], [country,code]]
print (answer)
else:
#but this is being ran instead
answer = [['country','code'], ['INVALID CODE', 'n/a']]
#return answer
If the first key-value pair looped over does not match, the else block is executed and you appear to be returning.
To look for a matching key in the dictionary, either test for it with in or use the .get() method to get the value or a default:
if search_key in countryDict:
answer = [['country','code'], [search_key, countryDict[search_key]]
else:
answer = [['country','code'], ['INVALID CODE', 'n/a']]
or use:
country = countryDict.get(search_key)
if country:
answer = [['country','code'], [country, search_key]]
else:
answer = [['country','code'], ['INVALID CODE', 'n/a']]
Related
Background
I have a list of "bad words" in a file called bad_words.conf, which reads as follows
(I've changed it so that it's clean for the sake of this post but in real-life they are expletives);
wrote (some )?rubbish
swore
I have a user input field which is cleaned and striped of dangerous characters before being passed as data to the following script, score.py
(for the sake of this example I've just typed in the value for data)
import re
data = 'I wrote some rubbish and swore too'
# Get list of bad words
bad_words = open("bad_words.conf", 'r')
lines = bad_words.read().split('\n')
combine = "(" + ")|(".join(lines) + ")"
#set score incase no results
score = 0
#search for bad words
if re.search(combine, data):
#add one for a hit
score += 1
#show me the score
print(str(score))
bad_words.close()
Now this finds a result and adds a score of 1, as expected, without a loop.
Question
I need to adapt this script so that I can add 1 to the score every time a line of "bad_words.conf" is found within text.
So in the instance above, data = 'I wrote some rubbish and swore too' I would like to actually score a total of 2.
1 for "wrote some rubbish" and +1 for "swore".
Thanks for the help!
Changing combine to just:
combine = "|".join(lines)
And using re.findall():
In [33]: re.findall(combine,data)
Out[33]: ['rubbish', 'swore']
The problem with having the multiple capturing groups as you originally were doing is that re.findall() will return each additional one of those as an empty string when one of the words is matched.
I have a program where the user is asked for the session year which needs to be in the form of 20XX-20XX. The constraint here is that it needs to be a year followed by its next year. Eg. 2019-2020.
For example,
Vaild Formats:
2019-2020
2018-2019
2000-2001
Invalid Fromats:
2019-2021
2000-2000
2019-2018
I am trying to validate this input using regular expressions.
My work:
import re
def add_pages(matchObject):
return "{0:0=3d}".format(int(matchObject) + 1)
try:
a = input("Enter Session")
p = r'2([0-9]{3})-2'
p1= re.compile(p)
x=add_pages(p1.findall(a)[0])
p2 = r'2([0-9]{3})-2'+x
p3 = re.compile(p2)
l=p3.findall(a)
if not l:
raise Exception
else:
print("Authenticated")
except Exception as e:
print("Enter session. Eg. 2019-2020")
Question:
So far I have not been able to retrieve a single regex that will validate this input. I did have a look at backreferencing in regex but it only solved half my query. I am looking for ways to improve this authentication process. Is there any single regex statement that will check for this constraint? Let me know if you need any more information.
Do you really need to get the session year in one input?
I think its better to have two inputs (or just automatically set the session year to be the first year + 1).
I don't know if you're aiming for something bigger and this is just an example but using regex just doesn't seem appropriate for this task to me.
For example you could do this:
print("Enter session year")
first_year = int(input("First year: "))
second_year = int(input("Second year: "))
if second_year != (first_year + 1):
# some validation
else:
# program continues
First of all, why regex? Regex is terrible at math. It would be easier to do something like:
def check_years(string):
string = "2011-2012"
years = string.split("-")
return int(years[0]) == (int(years[1]) - 1)
I'm having trouble converting my working code from lists to dictionaries. The basics of the code checks a file name for any keywords within the list.
But I'm having a tough time understanding dictionaries to convert it. I am trying to pull the name of each key and compare it to the file name like I did with lists and tuples. Here is a mock version of what i was doing.
fname = "../crazyfdsfd/fds/ss/rabbit.txt"
hollow = "SFV"
blank = "2008"
empty = "bender"
# things is list
things = ["sheep", "goat", "rabbit"]
# other is tuple
other = ("sheep", "goat", "rabbit")
#stuff is dictionary
stuff = {"sheep": 2, "goat": 5, "rabbit": 6}
try:
print(type(things), "things")
for i in things:
if i in fname:
hollow = str(i)
print(hollow)
if hollow == things[2]:
print("PERFECT")
except:
print("c-c-c-combo breaker")
print("\n \n")
try:
print(type(other), "other")
for i in other:
if i in fname:
blank = str(i)
print(blank)
if blank == other[2]:
print("Yes. You. Can.")
except:
print("THANKS OBAMA")
print("\n \n")
try:
print(type(stuff), "stuff")
for i in stuff: # problem loop
if i in fname:
empty = str(i)
print(empty)
if empty == stuff[2]: # problem line
print("Shut up and take my money!")
except:
print("CURSE YOU ZOIDBERG!")
I am able to get a full run though the first two examples, but I cannot get the dictionary to run without its exception. The loop is not converting empty into stuff[2]'s value. Leaving money regrettably in fry's pocket. Let me know if my example isn't clear enough for what I am asking. The dictionary is just short cutting counting lists and adding files to other variables.
A dictionary is an unordered collection that maps keys to values. If you define stuff to be:
stuff = {"sheep": 2, "goat": 5, "rabbit": 6}
You can refer to its elements with:
stuff['sheep'], stuff['goat'], stuff['rabbit']
stuff[2] will result in a KeyError, because the key 2 is not found in your dictionary. You can't compare a string with the last or 3rd value of a dictionary, because the dictionary is not stored in an ordered sequence (the internal ordering is based on hashing). Use a list or tuple for an ordered sequence - if you need to compare to the last item.
If you want to traverse a dictionary, you can use this as a template:
for k, v in stuff.items():
if k == 'rabbit':
# do something - k will be 'rabbit' and v will be 6
If you want to check to check the keys in a dictionary to see if they match part of a string:
for k in stuff.keys():
if k in fname:
print('found', k)
Some other notes:
The KeyError would be much easier to notice... if you took out your try/except blocks. Hiding python errors from end-users can be useful. Hiding that information from YOU is a bad idea - especially when you're debugging an initial pass at code.
You can compare to the last item in a list or tuple with:
if hollow == things[-1]:
if that is what you're trying to do.
In your last loop: empty == str(i) needs to be empty = str(i).
I am totally new to programming and I have been trying to get a simple piece of code working. However I keep getting multiple different errors so I assume I am doing something very wrong somewhere along the line... Here is my code at the moment:
userName = input('Please enter your name: ')
age = input('Please enter your age: ')
if int(age) <= 5:
print(userName, 'you are too young to play') break
else:
print (userName, 'Your old enough')
factor = 2
finalAge = int(age) + int(factor)
multAge = int(age) * int(factor)
divAge = float(age) / int(factor)
print('In', factor, 'years you will be', finalAge, 'years old', userName )
print('Your age multiplied by', factor, 'is', multAge)
print('Your age divided by', factor, 'is', divAge)
What I want to do is, if the user's age is not higher than 5, then they get the message that they are too young to play and it returns to the start of the piece of code - asking for the name again.
Does anyone have any advice on how to do this?
You'll need to use a loop. Syntax depends on language, which you haven't specified. As pseudo-code, you could do
loop indefinitely
prompt for name and age
if age is less than 5
print error
otherwise
print that age is ok
break loop
Take a look at while loops. For this you'd be able to set some kind of condition (such as an "old_enough" variable and when that becomes true, the loop stops running.
You would set this value inside the if statement. By default it should be whatever will make the loop run
There's loads of tutorials online for this, but here's an example in python (your code sample looks like Python3):
old_enough = False
while not old_enough:
# do something
if age > 5:
print("You're old enough")
old_enough = True
else:
print("you're not old enough")
That should make sense. if not, please do look up documentation. It'll be better for you in the long term
I'm attempting a few simple calculations in a def clean method following validation (basically spitting out a euro conversion of retrieved uk product price on the fly). I keep getting a TypeError.
Full error reads:
Cannot convert {'product': , 'invoice': , 'order_discount': Decimal("0.00"), 'order_price': {...}, 'order_adjust': None, 'order_value': None, 'DELETE': False, 'id': 92, 'quantity': 8} to Decimal
so I guess django is passing through the entire cleaned_data form to Decimal method. Not sure where I'm going wrong - the code I'm working with is:
def clean_order_price(self):
cleaned_data = self.cleaned_data
data = self.data
order_price = cleaned_data.get("order_price")
if not order_price:
try:
existing_price = ProductCostPrice.objects.get(supplier=data['supplier'], product_id=cleaned_data['product'], is_latest=True)
except ProductCostPrice.DoesNotExist:
existing_price = None
if not existing_price:
raise forms.ValidationError('No match found, please enter new price')
else:
if data['invoice_type'] == 1:
return existing_price.cost_price_gross
elif data['invoice_type'] == 2:
exchange = EuroExchangeRate.objects.latest('exchange_date')
calc = exchange.exchange_rate * float(existing_price.cost_price_gross)
calc = Decimal(str(calc))
return calc
return cleaned_data
If the invoice is of type 2 (a euro invoice) then the system should grab the latest exchange rate and apply that to the matching UK pound price pulled through to get euro result.
Should performing a decimal conversion be a problem within def clean method?
Thanks
I'm going to assume you've made an indentation error on pasting, and the lines from if data['invoice_type'] == 1: should actually be indented one level back - otherwise, as Alex says, the code will never get to the Decimal conversion.
There are multiple other problems with this code, but the biggest is that the last line returns the whole cleaned_data dictionary, rather than the value of this particular field - I suspect this is the cause of the error you are seeing.
Other than that, there is a big error where you calculate calc by multiplying cost_price_gross by exchange. Here exchange is an instance of EuroExchangeRate, rather than a number, so this calculation will not work.