Re: [Tutor] need a hint
I realize the code snippet was bad. It was meant to be pseudo code. I was on my phone and far from pc. Anyway I tried this: already_seen = set() for name in last_names: if name in already_seen: print("Already seen", name) else: already_seen.add(name) I am not seeing a pattern in the output to give me a clue as to why it is doing this. Also, it seems to be referencing chars when variable lastName is an item in a list. Unexpected output: Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> RESTART >>> Already seen s Already seen s Already seen k Already seen r Already seen o Already seen e Already seen i Already seen n Already seen l Already seen n Already seen e Already seen l Already seen r Already seen o Already seen s Already seen s Already seen o Already seen n Already seen l Already seen s Already seen n Already seen l Already seen t Already seen l Already seen k Already seen i Already seen r Already seen n Already seen l Already seen u Already seen e Already seen n Already seen l Already seen e Already seen h Already seen e Already seen t Already seen e Already seen e Already seen n Already seen e Already seen l Already seen i Already seen l Already seen i Already seen r Already seen a Already seen e Already seen e Already seen o Already seen e Already seen h Already seen e Already seen a Already seen t Already seen o Already seen n Already seen e Already seen r Already seen n Already seen e Already seen r Already seen r Already seen l Already seen e Already seen l Already seen e Already seen n Already seen o Already seen n Already seen r Already seen a Already seen s ['John Cornyn (R)', 'Ted Cruz (R)'] New Mexico Here is all my code: def createList( filename ): # print( filename ) senateInfo = {} try: info = open( filename, "r" ) for line in info: # print( line ) dataOnLine = line.split( "\t" ) state = dataOnLine[ 0 ] senator = dataOnLine[ 1 ] if state in senateInfo: # Adding another senator. # Create a list of the both senators from that state. incumbent = senateInfo[state] senators = [ incumbent, senator ] senateInfo[state] = senators else: senateInfo[state] = senator #print( senateInfo ) info.close() except: print( filename, " did not open! qUITTING." ) return senateInfo def createList2(filename): List = [] senateInfo2 = {} info = open( filename, "r" ) for line in info: dataOnLine = line.split( "\t" ) state = dataOnLine[ 0 ] senator = dataOnLine[ 1 ] nameSplit = dataOnLine[ 1 ].split(" ") if len(nameSplit) == 3: lastName = nameSplit[1] elif len(nameSplit) == 4: lastName = nameSplit[2] already_seen = set() for name in lastName: if name in already_seen: print("Already seen", name) else: already_seen.add(name) senateInfo2[lastName] = state info.close() return senateInfo2 def test( state, senatorsInfo ): print( senatorsInfo[state] ) def test2( senator, usSenators ): print( usSenators[senator] ) def main(): usSenators = createList( "USSenators.txt" ) usSenators2 = createList2( "USSenators.txt" ) test( "Texas", usSenators ) test2("Udall", usSenators2 ) main() On Tue, Dec 3, 2013 at 7:54 PM, Steven D'Aprano wrote: > On Tue, Dec 03, 2013 at 11:55:30AM -0600, Byron Ruffin wrote: > > What I am having trouble with is finding a way to say: if lastName > appears > > more than once, print something. > > > > I ran a bit of code: > > For x in lastname > > If lastname = udall > >Print something > > You most certainly did not run that. That's not Python code. Precision > and accuracy is vital when programming. Please tell us what you > *actually* ran, not some vague summary which may or may not be in the > right ballpark. > > Copy and paste is your friend here: copy and paste the block of code you > ran, don't re-type it from memory. > > > This prints x twice. > > > > I think what I might be hung up on is understanding the ways that I can > use > > a loop. I know I need to loop through the list of names, which I have, > and > > set a condition dor the apppearance of a string occurring more than once > in > > a list but I do
Re: [Tutor] need a hint
What I am having trouble with is finding a way to say: if lastName appears more than once, print something. I ran a bit of code: For x in lastname If lastname = udall Print something This prints x twice. I think what I might be hung up on is understanding the ways that I can use a loop. I know I need to loop through the list of names, which I have, and set a condition dor the apppearance of a string occurring more than once in a list but I don't know how to translate this to code. How do I say: if you see it twice, do something? On 2 December 2013 02:25, Byron Ruffin wrote: > > The following program works and does what I want except for one last problem > I need to handle. The program reads a txt file of senators and their > associated states and when I input the last name it gives me their state. > The problem is "Udall". There are two of them. The txt file is read by > line and put into a dictionary with the names split. I need a process to > handle duplicate names. Preferably one that will always work even if the > txt file was changed/updated. I don't want the process to handle the name > "Udall" specifically. For a duplicate name I would like to tell the user > it is not a unique last name and then tell them to enter first name and then > return the state of that senator. You're currently doing this: > senateInfo = {} > senateInfo[lastName] = state Instead of storing just a state in the dict you could store a list of states e.g.: senateInfo[lastName] = [state] Then when you find a lastName that is already in the dict you can do: senateInfo[lastName].append(state) to append the new state to the existing list of states. You'll need a way to test if a particular lastName is already in the dict e.g.: if lastName in senateInfo: Oscar ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] need a hint
The following program works and does what I want except for one last problem I need to handle. The program reads a txt file of senators and their associated states and when I input the last name it gives me their state. The problem is "Udall". There are two of them. The txt file is read by line and put into a dictionary with the names split. I need a process to handle duplicate names. Preferably one that will always work even if the txt file was changed/updated. I don't want the process to handle the name "Udall" specifically. For a duplicate name I would like to tell the user it is not a unique last name and then tell them to enter first name and then return the state of that senator. Thanks An excerpt of txt file... ArkansasMark Pryor (D)20032015 ArkansasJohn Boozman (R)20112017 CaliforniaDianne Feinstein (D)19922019 CaliforniaBarbara Boxer (D)19932017 ColoradoMark Udall (D)20092015 ColoradoMichael F. Bennet (D)20092017 def createList(state): senateInfo = {} info = open( "USSenators.txt", "r" ) for line in info: dataOnLine = line.split( "\t" ) state = dataOnLine[ 0 ] senator = dataOnLine[ 1 ] nameSplit = dataOnLine[ 1 ].split(" ") if len(nameSplit) == 3: lastName = nameSplit[1] elif len(nameSplit) == 4: lastName = nameSplit[2] senateInfo[lastName] = state info.close() return senateInfo def test( senator, usSenators ): print( usSenators[senator] ) def main(): usSenators = createList( "USSenators.txt" ) senator = input("Enter last name of Senator") test(senator, usSenators ) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] ideas?
Need a little help with finding a process for this: when a string of text is input, for example: abc def. I want to have each letter shift to the right one place in the alphabet. Thus.. abc def would be output as bcd efg. Any ideas on how to do this? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] loop running twice?
def main(): goal, apr, deposit = getSavingsDetails() determineMonthsTilSaved( goal, apr, deposit ) months = determineMonthsTilSaved(goal, apr, deposit) summarize( months ) def getSavingsDetails(): """ goal = float( input( "Principal sought? $" ) ) apr = float( input( "Interest rate? " ) ) deposit = float( input( "Deposit? $" ) ) """ goal = 1000.0 apr = .05 deposit = 100 return goal, apr, deposit def determineMonthsTilSaved( goal, apr, deposit ): months = 0 saved = 0 totalInterest = 0.0 while saved < goal: interest = saved * apr / 12 totalInterest += interest saved += (deposit + interest) months += 1 print( months, ("%.2f" % saved), ("%.2f" % totalInterest) ) return months def summarize( months ): print( "Saved! It took ", months // 12, "years and", months % 12, "months." ) main() When this is run it appears that determineMonthsTilSaved is running twice before the loop ends. It is supposed to run until saved > than goal, but look at the output. It runs again even after saved > goal. Help please? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] basic function concept
def main(x, y, z): print (x, y, z) def funct(): x = 1 y = 2 z = 3 return x, y, z main() Can someone tell me why main is not being given any arguments? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] space between words printed
The output generates a sentence made up of words chosen randomly from lists. I am having trouble getting a space between each of the words. Should I be thinking about .split? Here is the code (ignore indent errors as it was copied and pasted) Thank you: import random def wordList(): adj1 = ["Big","Small", "Early", "Late","Red", "Tall","Short"] subj = ["politician", "man","woman", "whale", "company", "child", "soldier"] obj = ["budget", "money", "box","gift","gun", "tank","drone"] adj2 = ["hot","crazy", "stupid", "fast","worthless", "awesome", "dirty"] verb = ["spends", "shoots", "evades", "pursues", "subverts", "passes", "flirts"] y = adj1[generate()], subj[generate()] + obj[generate()] + adj2[generate()] + verb[generate()] return y def generate(): random0_6 = random.randint(0, 6) return random0_6 def main(): print (wordList(), ".", sep="") main() ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] cs student needs help import math
I am writing a simple program based off an ipo chart that I did correctly. I need to use ceil but I keep getting an error saying ceil is not defined. I did import math, I think. I am using 3.2.3 and I imported this way... >>> import math >>> math.pi 3.141592653589793 >>> math.ceil(math.pi) 4 >>> math.floor(math.pi) 3 ... but I get the error when using ceil... pepsticks = ceil(peplength / StickLength) Traceback (most recent call last): File "", line 1, in pepsticks = ceil(peplength / StickLength) NameError: name 'ceil' is not defined Thanks for the help! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor