9 Replies - 683 Views - Last Post: 03 February 2012 - 08:53 AM Rate Topic: ***** 1 Votes

#1 ATC3  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 54
  • Joined: 04-October 11

Comparing strings inside variables

Posted 01 February 2012 - 03:14 PM

Okay so I am trying to compare these two strings for my new program:
job1= "test"
job2 = "test



So here is how I am comparing them:
if job1 == job2:
    print "Error"



It doesn't work. Does anybody know what I can do to fix it? I am using python 2.7. Is there something I don't know about that you use when comparing strings? This is not a homework assignment, just a personal project.

Is This A Good Question/Topic? 0
  • +

Replies To: Comparing strings inside variables

#2 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 318
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Comparing strings inside variables

Posted 01 February 2012 - 06:34 PM

How doesn't it work? Could you tell us the error? Could it be that there is no second closing apostrophe after job2?
Was This Post Helpful? 2
  • +
  • -

#3 ATC3  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 54
  • Joined: 04-October 11

Re: Comparing strings inside variables

Posted 02 February 2012 - 07:52 AM

No that was a typo. It should print error, right? But it does not. I print these functions later in the script and it will just print both as "test". I have the if statement in the right position too. IDK what the problem is.
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2017
  • View blog
  • Posts: 3,046
  • Joined: 21-June 11

Re: Comparing strings inside variables

Posted 02 February 2012 - 08:00 AM

After fixing the missing " the code you showed us does print Error. If you ran that exact code (after fixing the missing ") and it did not print Error, your installation of python must be buggy.

If the code you've shown us is actually not the same code that you are running, the problem is in the code you're actually running. Of course we can't tell you what the problem is because the code that you've shown us did not contain the problem.
Was This Post Helpful? 1
  • +
  • -

#5 ATC3  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 54
  • Joined: 04-October 11

Re: Comparing strings inside variables

Posted 02 February 2012 - 08:11 AM

Your right it does work. The way I am implementing it is inside a function. I get a random name from the array, and assign it to a variable like job1 and job2. Then before I print it I check to see if job2 is the same as job1 to avoid duplicates in the list of jobs it is generating. I know this code is not optimal but I am going to fix it later. I just want to get this problem resolved:

def job_generator():
    import random
    first = ["test"]
    third = ["test"]
    fifth = ["test"]
    seventh = ["test"]
    ninth = ["test"]
    randomNumber1 = random.randrange(0,len(first))
    randomNumber3 = random.randrange(0,len(third))
    randomNumber5 = random.randrange(0,len(fifth))
    randomNumber7 = random.randrange(0,len(seventh))
    randomNumber9 = random.randrange(0,len(ninth))
    job1 = first[randomNumber1]
    job2 = third[randomNumber3]
    job3 = fifth[randomNumber5]
    job4 = seventh[randomNumber7]
    job5 = ninth[randomNumber9]
    print(job1)
    print "\t"
    if job1 == job2:
		job2 = "error"
    print(job2)
    print "\t"
    print (job3)
    print "\t"
    print (job4)
    print "\t"
    print (job5)
 
job_generator()
stop = input()




The problem is found at about lines 20, and 21.

This post has been edited by ATC3: 02 February 2012 - 08:14 AM

Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2017
  • View blog
  • Posts: 3,046
  • Joined: 21-June 11

Re: Comparing strings inside variables

Posted 02 February 2012 - 08:29 AM

That code produces the following output for me:

test
        
error
        
test
        
test
        
test



Which as far as I can tell is exactly what you want.
Was This Post Helpful? 1
  • +
  • -

#7 ATC3  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 54
  • Joined: 04-October 11

Re: Comparing strings inside variables

Posted 02 February 2012 - 08:37 AM

I have no idea why it was not working for me before. Thanks anyway.
Was This Post Helpful? 0
  • +
  • -

#8 ATC3  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 54
  • Joined: 04-October 11

Re: Comparing strings inside variables

Posted 02 February 2012 - 08:46 AM

I got the concept to work but this won't work in the program:
def job_generator():
    import random
    first = ["Adan", "Alex", "Vladimir", "Alfredo", "Carlos", "Justin", "Martin", "Cory", "Jeff", "Reginaldo", "Valerio", "Joel"]
    third = ["Adan", "Alex", "Vladimir", "Alfredo", "Carlos", "Justin", "Martin", "Cory", "Jeff", "Reginaldo", "Valerio"]
    fifth = ["Adan", "Alex", "Cory", "Jeff", "Valerio", "Justin"]
    seventh = ["Adan", "Alex", "Vladimir", "Alfredo", "Carlos", "Justin", "Martin", "Cory", "Jeff", "Reginaldo", "Valerio"]
    ninth = ["Adan", "Cory" , "Martin", "Valerio", "Cory", "Jeff", "Vladimir"]
    randomNumber1 = random.randrange(0,len(first))
    randomNumber3 = random.randrange(0,len(third))
    randomNumber5 = random.randrange(0,len(fifth))
    randomNumber7 = random.randrange(0,len(seventh))
    randomNumber9 = random.randrange(0,len(ninth))
    job1 = first[randomNumber1] + " is assigned to Mic 1"
    job2 = third[randomNumber3] + " is assigned to Mic 2"
    job3 = fifth[randomNumber5] + " is assigned to Sound "
    job4 = seventh[randomNumber7] + " is assigned to Stage"
    job5 = ninth[randomNumber9] + " is assigned to Attendant"
    print "\t"
    print(job1)
    print "\t"
    if job1 == job2:
		randomNumber3 = random.randrange(0,len(third))
		job2 = third[randomNumber3] = " is assigned to Mic 2"
    print(job2)
    print "\t"
    print (job3)
    print "\t"
    print (job4)
    print "\t"
    print (job5)
 
job_generator()






Was This Post Helpful? 0
  • +
  • -

#9 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2017
  • View blog
  • Posts: 3,046
  • Joined: 21-June 11

Re: Comparing strings inside variables

Posted 03 February 2012 - 03:05 AM

Are you sure that the if isn't executed in that code? If you're just basing that on the fact that sometimes job2 will be equal to job1, that doesn't tell you anything. It's perfectly possible for job2 to be equal to job1 after the if statement executed - just less likely (the call to randrange inside the if statement isn't guaranteed to return a different number than the previous call).

PS: Instead of using the result of randrange as an index into the list, you should just use random.choice(list). That would get rid of all your randomNumbers. You also may want to use a list of lists for the names and then a list of strings for the jobs.
Was This Post Helpful? 0
  • +
  • -

#10 ATC3  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 54
  • Joined: 04-October 11

Re: Comparing strings inside variables

Posted 03 February 2012 - 08:53 AM

Good suggestion. I will use that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1