Reputation: 13
I've been working on a login and signup code that is not connected to the cloud. To login, you must enter a username and password, and it will return you with a simple; "Hello, "
I've tried converting the element to a string, but I couldn't find anything else online to help me. It could possibly be that the program I'm using doesn't display an output properly.
inorup=""
newuser=""
users=["jdoe,tractor5","carrey1,1997","admin,56f67dk2997m"]
firstnuser=["John","Carrey","Frederick"]
while inorup != "login" or inorup != "signup":
inorup=raw_input("Would you like to login or signup? ")
if inorup=="login":
userpass=raw_input("Enter <username,password>: ")
if userpass in users:
userpassfnamepos=int(users.index(userpass))-3
print("Hello",str(firstnuser[userpassfnamepos])+".")
#Returning:
Would you like to login or signup? login
Enter <username,password>: jdoe,tractor5
('Hello', 'John')
#Expected Return:
Would you like to login or signup? login
Enter <username,password>: jdoe,tractor5
Hello John.
Upvotes: 0
Views: 51
Reputation: 464
Looks like you are mixing python 2 and 3. raw_input id python 2 and in Python 3 it's only input. also, you should use brake to break out of the loop at appropriate places. You have an infinite loop. Lately, the code is printing "Hello John" for me as it's supposed to. However, you don't have to convert the list element to string, it is already a string (therefore returns the same value with or without converting.
I think the reason it's giving you ('Hello', 'John') as output is because it's printing the parenthesis themselves (but that does not make logical sense because they are not included in the string. Anyways note that python 2 print statement is as follows: print "this is to be printed" . there is no parenthesis. Make use of the vertion of python you are using and that your code matches the version.
One additional note, if you are not using a database, maybe use a python dictionary instead of a python list. Not only it's faster and memory efficient, it also makes it easier to work with. it can be something like {username : ("name" , "password")} a combination of a dictionart and a tuple or more explicit {username : {name: "name" , password: "password"}}, which saves a sigle's user data in a dictionary inside a dectionary containing all the users.
Upvotes: 0
Reputation:
You are already using normal string concatenation to add the period. Why not just do that with the name as well? You also don't need the str
in front, since the list is already a list of strings, and so the element in question is already a string.
print("Hello" + " " + firstnuser[userpassfnamepos] + ".")
>>>Would you like to login or signup? login
>>>Enter <username,password>: jdoe,tractor5
>>>Hello John.
Upvotes: 0
Reputation: 846
You should split your string on the ',' and check username password against a dictionary like {username:password}, or use a database or something. Checking string as you do now is kind of limiting and unwieldy.
But to just fix your output, you can change your print to this
print("Hello {}.".format(firstnuser[userpassfnamepos]))
using string.format() also does all the casting and such so you could pass in numbers or whatever you want, not just name. More info Here
and you will get the expected output.
output =
Would you like to login or signup? login
Enter : jdoe,tractor5
Hello John.
Upvotes: 1