How do I check user input from a file in Python? - python-3.x

Let's say I have a text file:
Alice,Bob,Charles,David,Emily,Frank
I want to check if a string that the user enters matches a name in the text file, something like this:
Name = str(input("Enter a name here: ")
if Name == Alice:
print("Foo")
elif Name == Bob:
print("Bar")
How do I do it?

Assuming that you want to check if a string that the user enters matches a name in the text file then you should read your text file in advance and then compare to the user input as follows
names = [i.split(',') for i in open('names.txt', 'r')][0]
Name = input()
[name+' is in the list' for name in names if name == Name]
Assuming that you want to check if a string that the user enters matches a name in the name of a text file then you should read your text file names in advance and then compare to the user input as follows:
names = os.listdir(directory)
Name = input()
[name+' is in the list' for name in names if name == Name]

lets assume that your directory looks something like this :
$cd folder
$ls
Alice Bob Charles David Emily Frank
Then you can do it by.
Name = str(input("Enter a name here: ")
if Name in os.listdir('folder'):
print("Name found!!!! {}".format(Name))

Related

Creating a greeting program which only accepts specific users and invalidates any others

I have just started to learn python and am trying to solve a basic elemenatary problem.
I would like to ask the user to input their name. There are only two valid names (Bob and Alice) that should receive a greeting. Any users who do not go by those names should receive an incorrect entry message.
I have created a list of names which include Bob and Alice. I have added a code to prompt the user to input their name.
I have written a print statement which greets the user after inputting the name.
The difficulty I am having is knowing which function I should use to invalidate any users that aren't Bob and Alice. I have written the following code;
names = ("Alice", "Bob", "Ray")
name = input("What is your name?")
if name == "Alice" "Bob":
print("Hello", name)
else:
print("Invalid entry")
My code is only ending in invalid entry, regardless of which name is used.
The problem is here:
if name == "Alice" "Bob":
Python concatenates strings that are one after the other with nothing in between, so it effectively checks whether the name is "AliceBob". Just check what it prints if you type it.
What you want to do is a check like this:
if name == "Alice" or name == "Bob":
or like this:
if name in ("Alice", "Bob"):

Why I am getting a duplicate output in Python?

I am trying the below code. But in the final output, I am getting repeated words. For example, if I input name as Jai, I will get JaiJai.
name = input ("Cheer: ")
for i in name:
name +=i
print('Give me a', i+",", i+"!")
print("What does it spell?")
print(name)
Because of this:
for i in name:
name +=i
for every character in a given word, add this character to the word.
You adding up the value of i to the name variable
this line name +=i is redundant here :)
corrected code:
name = input ("Cheer: ")
for i in name:
print('Give me a', i+",", i+"!")
print("What does it spell?")
print(name)

i want to delete the element even if user input is in small or Capital letter

nametoRemove = input("Enter the name you want to remove:")
print(nametoRemove)
name = ["Ramesh","Rakesh","Suresh"]
name.remove(nametoRemove)
print(name)
you can do it fith basic for loop and if statement:
nametoRemove = input("Enter the name you want to remove:")
print(nametoRemove)
name = ["Ramesh","Rakesh","Suresh"]
for item in name:
if item.lower() == nametoRemove.lower():
name.remove(item)
print(name)
output:
Enter the name you want to remove:ramesh
ramesh
['Rakesh', 'Suresh']

How do i display an input in Python within a print statement

name = input('What is You name ')
this shows me only the input, I need to display 'hello' as well!!
print ('Hello', input(name))
When I try to type this :-
name = input('What is You name ')
print ('Hello')
input(name)
Why is that it displays the name directly and not the hello keyword. Could anyone please update on the same and share a coed that might be helpful.
This is even the exact example in the docs
person = input('Enter your name: ')
print('Hello', person)

Storing multiple passwords

I need to be able to store multiple usernames in a separate text file and access each username individually.
My code currently writes them all on the same line
This is what i have got
start=input("Please enter either Login or Signup.")
if start=="Signup":
First_name=input("Please enter your first name")
Last_name=input("Please enter your last name")
age=input("Please enter your age")
Usernames=open("Username_file","r")
Username1=(Usernames.read())
Username=(First_name[0:3])+age
Usernames=open("Username_file","w")
Usernames.write(Username1)
Usernames.write(Username)
Usernames.close()
Usernames=open("Username_file","r")
for line in Usernames:
print (line)
You are going about it the wrong way:
If you just want to add their first and last names to the end of the file 'Username_file.txt', you are doing it too complicatedly. Currently, your reading in the entire file to a variable and then writing the contents of the file before (from that variable) and the inputted name back to the same file.
The docs state that a is an option for open() which allows you to append to the file. This means you can just write the new username on without having to open it twice writing the same thing.
To do this, your code could look something like:
start = input("Please enter either Login or Signup: ")
if start == "Signup":
First_name = input("Please enter your first name: ")
Last_name = input("Please enter your last name: ")
age = input("Please enter your age: ")
with open("Username_file.txt", "a") as f:
f.write(First_name[0:3]+age+"\n")
with open("Username_file.txt", "r") as f:
for line in f:
print(line)

Resources