i have a computer science project and i can't find anywhere how to figure this out. I need python to ask for input and if input is equal to a text file then it moves onto next input if it's the same. If not it repeats the question: “please enter your name” so far I've got:
Player_1 = input(“please enter your name”)
Then need what I've said above. The text file only needs to contain two names. I am relatively new to python, hope someone can help.
Your text file "filename.txt" should use a symbol to separate names.
Let's say a comma, so your file should look like this:
filename.txt: Name1,Name2
You could use:
txt=open("filename.txt",'r').read()
to get your file content as string.
namesList=txt.split(',')
to get a list of the names in the file:
["Name1","Name2"]
then a loop to check inputs of the user:
userEntry=''
while userEntry not in namesList:
userEntry=input("please enter your name")
At the end, userEntry is equal to one of the two names in your file.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.