Just started programming (Python), using content of text file (x) to satisfy input() == x
Using Python I am trying to get the content of a text file to to be variable x and by using a if statement, create a simple password program through the use of user input which is the s variable. However, when I make the user input 1234 which is the same as the text.txt file, the if statement fails and 'Access Denied' is printed. Any help would be greatly appreciated.
Here is my code:
print ('Enter Password')
s = input() #goal is to input '1234'
x = open('text.txt','r') #text.txt contains '1234'
print("you typed", s)
if s == x:
print("Access Granted")
else:
print("Access Denied")
python if-statement
add a comment |
Using Python I am trying to get the content of a text file to to be variable x and by using a if statement, create a simple password program through the use of user input which is the s variable. However, when I make the user input 1234 which is the same as the text.txt file, the if statement fails and 'Access Denied' is printed. Any help would be greatly appreciated.
Here is my code:
print ('Enter Password')
s = input() #goal is to input '1234'
x = open('text.txt','r') #text.txt contains '1234'
print("you typed", s)
if s == x:
print("Access Granted")
else:
print("Access Denied")
python if-statement
1
text.txtneeds to only have the characters1234. If it has anything else including white space or new lines or whitespace this will fail. Check the value ofxby printing it out and make sure it's what you expect.
– Spencer Wieczorek
Nov 15 '18 at 4:26
add a comment |
Using Python I am trying to get the content of a text file to to be variable x and by using a if statement, create a simple password program through the use of user input which is the s variable. However, when I make the user input 1234 which is the same as the text.txt file, the if statement fails and 'Access Denied' is printed. Any help would be greatly appreciated.
Here is my code:
print ('Enter Password')
s = input() #goal is to input '1234'
x = open('text.txt','r') #text.txt contains '1234'
print("you typed", s)
if s == x:
print("Access Granted")
else:
print("Access Denied")
python if-statement
Using Python I am trying to get the content of a text file to to be variable x and by using a if statement, create a simple password program through the use of user input which is the s variable. However, when I make the user input 1234 which is the same as the text.txt file, the if statement fails and 'Access Denied' is printed. Any help would be greatly appreciated.
Here is my code:
print ('Enter Password')
s = input() #goal is to input '1234'
x = open('text.txt','r') #text.txt contains '1234'
print("you typed", s)
if s == x:
print("Access Granted")
else:
print("Access Denied")
python if-statement
python if-statement
asked Nov 15 '18 at 4:23
Jacob Jacob
91
91
1
text.txtneeds to only have the characters1234. If it has anything else including white space or new lines or whitespace this will fail. Check the value ofxby printing it out and make sure it's what you expect.
– Spencer Wieczorek
Nov 15 '18 at 4:26
add a comment |
1
text.txtneeds to only have the characters1234. If it has anything else including white space or new lines or whitespace this will fail. Check the value ofxby printing it out and make sure it's what you expect.
– Spencer Wieczorek
Nov 15 '18 at 4:26
1
1
text.txt needs to only have the characters 1234. If it has anything else including white space or new lines or whitespace this will fail. Check the value of x by printing it out and make sure it's what you expect.– Spencer Wieczorek
Nov 15 '18 at 4:26
text.txt needs to only have the characters 1234. If it has anything else including white space or new lines or whitespace this will fail. Check the value of x by printing it out and make sure it's what you expect.– Spencer Wieczorek
Nov 15 '18 at 4:26
add a comment |
2 Answers
2
active
oldest
votes
In your version, x is a file object. To get the content you need to read it. It's a good idea to strip the whitespace too
print ('Enter Password')
s = input() #goal is to input '1234'
x = open('text.txt','r').read().strip()
print("you typed", s)
if s == x:
print("Access Granted")
else:
print("Access Denied")
But now you have an open file that you can't close (Python will usually do it for you but it's better to start doing these things properly). The usual way would be to use with
print ('Enter Password')
s = input() #goal is to input '1234'
with open('text.txt','r') as fin:
x = fin.read().strip()
print("you typed", s)
if s == x:
print("Access Granted")
else:
print("Access Denied")
Now the file is closed automatically at the end of the with block (This is called a context manager)
python is really... really really good at garbage collection ... especially when you just open a file for reading ... solid advice all the same though
– Joran Beasley
Nov 15 '18 at 4:36
add a comment |
The output of open assigned to variable x will be a file object
Try to print x, you will see something like below
<_io.TextIOWrapper name='text.txt' mode='r' encoding='UTF-8'>
For the content of the file you can use .readline(). It reads one line from file object.
Then you will have a newline at the end to take care of .strip() does the job of removing the newline at the end.
Following code works
print ('Enter Password')
s = input() #goal is to input '1234'
x = open('text.txt', 'r').readline().rstrip() #text.txt contains '1234'
print("you typed", s)
print("file content is", x)
if s == x:
print("Access Granted")
else:
print("Access Denied")
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312409%2fjust-started-programming-python-using-content-of-text-file-x-to-satisfy-inp%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
In your version, x is a file object. To get the content you need to read it. It's a good idea to strip the whitespace too
print ('Enter Password')
s = input() #goal is to input '1234'
x = open('text.txt','r').read().strip()
print("you typed", s)
if s == x:
print("Access Granted")
else:
print("Access Denied")
But now you have an open file that you can't close (Python will usually do it for you but it's better to start doing these things properly). The usual way would be to use with
print ('Enter Password')
s = input() #goal is to input '1234'
with open('text.txt','r') as fin:
x = fin.read().strip()
print("you typed", s)
if s == x:
print("Access Granted")
else:
print("Access Denied")
Now the file is closed automatically at the end of the with block (This is called a context manager)
python is really... really really good at garbage collection ... especially when you just open a file for reading ... solid advice all the same though
– Joran Beasley
Nov 15 '18 at 4:36
add a comment |
In your version, x is a file object. To get the content you need to read it. It's a good idea to strip the whitespace too
print ('Enter Password')
s = input() #goal is to input '1234'
x = open('text.txt','r').read().strip()
print("you typed", s)
if s == x:
print("Access Granted")
else:
print("Access Denied")
But now you have an open file that you can't close (Python will usually do it for you but it's better to start doing these things properly). The usual way would be to use with
print ('Enter Password')
s = input() #goal is to input '1234'
with open('text.txt','r') as fin:
x = fin.read().strip()
print("you typed", s)
if s == x:
print("Access Granted")
else:
print("Access Denied")
Now the file is closed automatically at the end of the with block (This is called a context manager)
python is really... really really good at garbage collection ... especially when you just open a file for reading ... solid advice all the same though
– Joran Beasley
Nov 15 '18 at 4:36
add a comment |
In your version, x is a file object. To get the content you need to read it. It's a good idea to strip the whitespace too
print ('Enter Password')
s = input() #goal is to input '1234'
x = open('text.txt','r').read().strip()
print("you typed", s)
if s == x:
print("Access Granted")
else:
print("Access Denied")
But now you have an open file that you can't close (Python will usually do it for you but it's better to start doing these things properly). The usual way would be to use with
print ('Enter Password')
s = input() #goal is to input '1234'
with open('text.txt','r') as fin:
x = fin.read().strip()
print("you typed", s)
if s == x:
print("Access Granted")
else:
print("Access Denied")
Now the file is closed automatically at the end of the with block (This is called a context manager)
In your version, x is a file object. To get the content you need to read it. It's a good idea to strip the whitespace too
print ('Enter Password')
s = input() #goal is to input '1234'
x = open('text.txt','r').read().strip()
print("you typed", s)
if s == x:
print("Access Granted")
else:
print("Access Denied")
But now you have an open file that you can't close (Python will usually do it for you but it's better to start doing these things properly). The usual way would be to use with
print ('Enter Password')
s = input() #goal is to input '1234'
with open('text.txt','r') as fin:
x = fin.read().strip()
print("you typed", s)
if s == x:
print("Access Granted")
else:
print("Access Denied")
Now the file is closed automatically at the end of the with block (This is called a context manager)
answered Nov 15 '18 at 4:28
John La RooyJohn La Rooy
213k41276429
213k41276429
python is really... really really good at garbage collection ... especially when you just open a file for reading ... solid advice all the same though
– Joran Beasley
Nov 15 '18 at 4:36
add a comment |
python is really... really really good at garbage collection ... especially when you just open a file for reading ... solid advice all the same though
– Joran Beasley
Nov 15 '18 at 4:36
python is really... really really good at garbage collection ... especially when you just open a file for reading ... solid advice all the same though
– Joran Beasley
Nov 15 '18 at 4:36
python is really... really really good at garbage collection ... especially when you just open a file for reading ... solid advice all the same though
– Joran Beasley
Nov 15 '18 at 4:36
add a comment |
The output of open assigned to variable x will be a file object
Try to print x, you will see something like below
<_io.TextIOWrapper name='text.txt' mode='r' encoding='UTF-8'>
For the content of the file you can use .readline(). It reads one line from file object.
Then you will have a newline at the end to take care of .strip() does the job of removing the newline at the end.
Following code works
print ('Enter Password')
s = input() #goal is to input '1234'
x = open('text.txt', 'r').readline().rstrip() #text.txt contains '1234'
print("you typed", s)
print("file content is", x)
if s == x:
print("Access Granted")
else:
print("Access Denied")
add a comment |
The output of open assigned to variable x will be a file object
Try to print x, you will see something like below
<_io.TextIOWrapper name='text.txt' mode='r' encoding='UTF-8'>
For the content of the file you can use .readline(). It reads one line from file object.
Then you will have a newline at the end to take care of .strip() does the job of removing the newline at the end.
Following code works
print ('Enter Password')
s = input() #goal is to input '1234'
x = open('text.txt', 'r').readline().rstrip() #text.txt contains '1234'
print("you typed", s)
print("file content is", x)
if s == x:
print("Access Granted")
else:
print("Access Denied")
add a comment |
The output of open assigned to variable x will be a file object
Try to print x, you will see something like below
<_io.TextIOWrapper name='text.txt' mode='r' encoding='UTF-8'>
For the content of the file you can use .readline(). It reads one line from file object.
Then you will have a newline at the end to take care of .strip() does the job of removing the newline at the end.
Following code works
print ('Enter Password')
s = input() #goal is to input '1234'
x = open('text.txt', 'r').readline().rstrip() #text.txt contains '1234'
print("you typed", s)
print("file content is", x)
if s == x:
print("Access Granted")
else:
print("Access Denied")
The output of open assigned to variable x will be a file object
Try to print x, you will see something like below
<_io.TextIOWrapper name='text.txt' mode='r' encoding='UTF-8'>
For the content of the file you can use .readline(). It reads one line from file object.
Then you will have a newline at the end to take care of .strip() does the job of removing the newline at the end.
Following code works
print ('Enter Password')
s = input() #goal is to input '1234'
x = open('text.txt', 'r').readline().rstrip() #text.txt contains '1234'
print("you typed", s)
print("file content is", x)
if s == x:
print("Access Granted")
else:
print("Access Denied")
answered Nov 15 '18 at 4:30
KrishnaKrishna
6021515
6021515
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312409%2fjust-started-programming-python-using-content-of-text-file-x-to-satisfy-inp%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
text.txtneeds to only have the characters1234. If it has anything else including white space or new lines or whitespace this will fail. Check the value ofxby printing it out and make sure it's what you expect.– Spencer Wieczorek
Nov 15 '18 at 4:26