Just started programming (Python), using content of text file (x) to satisfy input() == x










-1















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")









share|improve this question

















  • 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
















-1















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")









share|improve this question

















  • 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














-1












-1








-1








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")









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 4:23









Jacob Jacob

91




91







  • 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













  • 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








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













2 Answers
2






active

oldest

votes


















1














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)






share|improve this answer























  • 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


















0














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")





share|improve this answer






















    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
    );



    );













    draft saved

    draft discarded


















    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









    1














    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)






    share|improve this answer























    • 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















    1














    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)






    share|improve this answer























    • 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













    1












    1








    1







    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)






    share|improve this answer













    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)







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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

















    • 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













    0














    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")





    share|improve this answer



























      0














      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")





      share|improve this answer

























        0












        0








        0







        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")





        share|improve this answer













        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")






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 4:30









        KrishnaKrishna

        6021515




        6021515



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            What does pagestruct do in Eviews?

            Dutch intervention in Lombok and Karangasem

            Channel Islands