Simple if statement...

Check if strings are the same...

Page 1 of 1

3 Replies - 1454 Views - Last Post: 13 May 2009 - 11:03 AM Rate Topic: -----

#1 Scorpiion  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 117
  • Joined: 23-April 09

Simple if statement...

Posted 10 May 2009 - 05:53 AM

Hi I have just started out in python and have a small python program that I doing for class... I have this one file named passwd.txt and then my program that ask for a username and a password. I have successfully read in the data and put into string also the passwd.txt is read in and put into strings. I just need to check if the right username and password is typed in...

You can see how I want the if statement to work, but please tell me why it does not work and how I can do it right... :)

passwd.txt
name=Kalle Anka:user=kalle:passw=rally
name=Svante Carlsson:user=svante:passw=hopp



Program
# Open passwd.txt
passwd = open("passwd.txt", "r")

# Prompt for userdata
user = raw_input("Username: ")
password = raw_input("Password: ")

line1 = passwd.readline()
line2 = passwd.readline()

dict1 = line1.split(":")
dict2 = line2.split(":")

check_1_user = dict1[1]
check_1_pass = dict1[2]
check_2_user = dict2[1]
check_2_pass = dict2[2]

check_1_user = check_1_user[5:]
check_1_pass = check_1_pass[6:]
check_2_user = check_2_user[5:]
check_2_pass = check_2_pass[6:]

if (user == check_1_user) or (user == check_2_user):
 if (password == check_1_pass) or (password == check_2_pass):
   print "Okey, you have access to the secrete page..."
else:
 print "You have not access..."

# Close passwd.txt
passwd.close()



Is This A Good Question/Topic? 0
  • +

Replies To: Simple if statement...

#2 Scorpiion  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 117
  • Joined: 23-April 09

Re: Simple if statement...

Posted 10 May 2009 - 07:09 AM

I know what my problem is now... there is some "\n" in the string that I read in from the file... but how can I remove that?
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,548
  • Joined: 23-August 08

Re: Simple if statement...

Posted 11 May 2009 - 05:23 AM

print "this is mystring\n".rstrip()

Was This Post Helpful? 0
  • +
  • -

#4 paathos  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 02-March 09

Re: Simple if statement...

Posted 13 May 2009 - 11:03 AM

Just string.strip() will remove any trailing whitespace, so the easiest way is to say check1 = check1[5:].strip().
But what if you have more users, or more passwords? You should do something like
lines = open('passwd.txt', 'r').readlines()
Then when a user enters their name and password, you would loop through all lines to see if there was a match or not.
lines = open('passwd.txt', 'r').readlines()
good = False
for line in lines:
	 data = line.split(':')
	 if user == data[0][5:].strip() and passw == data[1][6:].strip():
		  good  = True
if good:
	 Yea!
else:
	 Boo!


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1