Python Forum
Cannot read from text file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cannot read from text file
#1
So I am new to coding and have been instructed to create a program that stores information in a database and can retrieve it at the users command. However, I am having trouble reading the database.
question = input("Would you like to view or create a new record? \n A: Create a new record \n B: View a record  ")
if question=="B":
    f = open("Database (2).txt","r")
    file_contents=f.read()
    print(file_contents)
    f.close()
    
When the user selects to retrieve a record, it simply continues as if "A" has been selected, where the program asks the user for information. Here is "A"
if question=="A" or "a":
    firstname = input("Please enter your first name:  ")
    surname = input("Welcome, please enter your surname:  ")
    year = int(input("Please enter the year you were born:  "))
    month = input("Please enter the month you were born:  ")
    day = input("Please enter the day you were born:  ")
    gender = input("Please enter your gender:")
I am also experiencing an error when the program asks whether you would like to view the information you just entered. For example, when stating that you would not like to view the information, it would continue to ask what information you would like to see. Here is the section of the code:

option=input("If you would like to view your record, type 'yes' or type 'no' to display message")
if option=="yes" or "Yes":
    print("What information would you like to view?")
    info=input("Please select: Age, Gender or Fullname. If you would like to view all your information, type 'all'")
    if info == "Age":
        print("You are",age)
    elif info == "Gender":
        print("You are",gender)
    elif info == "Fullname":
        print("Your name is",fullname)
    else:
        print("You are",age)
        print("You are",gender)
        print("Your name is",fullname)
if option =="no" or "No":
    print("Thank you for using our database")
Here is what I mean (Taken from when the program is running)

"If you would like to view your record, type 'yes' or type 'no' to display message:no
What information would you like to view?
Please select: Age, Gender or Fullname. If you would like to view all your information, type 'all'"

And here is my complete code, incase any of the errors are hidden within a section I did not include:

import csv
print("Please enter your password that has been e-mailed to you:")
password = "stone"
entry=input()
#while loop
while entry != password:
    print("Password incorrect")
    entry=input("Please input your password")
    if entry == password:
        print("Your password is correct")
question = input("Would you like to view or create a new record? \n A: Create a new record \n B: View a record  ")
if question=="B":
    f = open("Database (2).txt","r")
    file_contents=f.read()
    print(file_contents)
    f.close()
    
    
            
if question=="A" or "a":
    firstname = input("Please enter your first name:  ")
    surname = input("Welcome, please enter your surname:  ")
    year = int(input("Please enter the year you were born:  "))
    month = input("Please enter the month you were born:  ")
    day = input("Please enter the day you were born:  ")
    gender = input("Please enter your gender:")

fullname=firstname + surname
print("Your name is",fullname)
age=2017 - year

print("You are",age,"years old")
print("You are",gender)

with open("Database(2).txt", "a") as file:
    filewriter=csv.writer(file)
    filewriter.writerow([firstname,surname,age,gender])

    
option=input("If you would like to view your record, type 'yes' or type 'no' to display message")
if option=="yes" or "Yes":
    print("What information would you like to view?")
    info=input("Please select: Age, Gender or Fullname. If you would like to view all your information, type 'all'")
    if info == "Age":
        print("You are",age)
    elif info == "Gender":
        print("You are",gender)
    elif info == "Fullname":
        print("Your name is",fullname)
    else:
        print("You are",age)
        print("You are",gender)
        print("Your name is",fullname)
if option =="no" or "No":
    print("Thank you for using our database")
    
If possible, could someone please teach me how to add a limit to the number of times an incorrect password can be entered please?
Thank you, I hope I can reach a solution to please my teacher as she described my work as a "Disappointing effort" Sad
Reply
#2
Quote:
if question=="A" or "a":

Let's say they type in "B".  question=="A" is false, but "a" is still "a".  We can rewrite the if statement, then, to mean this: if False or "a":.  And a non-empty string is always truthy, which is why your "A" block is executing.

What you probably want is if question == "A" or question == "a":, or if question in ("A", "a"):, or if question.lower() == "a":.

To help demonstrate:
>>> spam = "foo"
>>> spam == "bar"
False
>>> spam == "bar" or "eggs"
'eggs'
Reply
#3
Ok thank you for your reply. I have done what you said however I am now getting a syntax error.
"fullname=firstname + surname
NameError: name 'firstname' is not defined"

I am not sure what this means as I have defined "firstname" here:
if question == "A" or question == "a":
    firstname = input("Please enter your first name:  ")
Reply
#4
So firstname is sometimes defined.  If you don't type "A", then it's never defined.  Try this:
firstname = ""
if question == "A" or question == "a":
    firstname = input("Please enter your first name: ")
firstname = firstname + surname
print(firstname)
...you'll likely have to do something similar for the other variables.
Reply
#5
ahhh I see, thank you. So is there any reason why, when asked if I would like to view the information that I just entered (I say no), it continues as if I have said yes? Please see code above. Thanks
Reply
#6
Exactly the same reason as before.
https://python-forum.io/Thread-Cannot-re...2#pid27112
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  computer science coursework, read the text please and tell me if theres any specifics sixcray 4 2,604 Nov-11-2020, 03:17 PM
Last Post: buran
  Can we store value in file if we open file in read mode? prasanthbab1234 3 2,548 Sep-26-2020, 12:10 PM
Last Post: ibreeden
  [split] how to read a specific row in CSV file ? laxmipython 2 8,854 May-22-2020, 12:19 PM
Last Post: Larz60+
  Read data from a CSV file in S3 bucket and store it in a dictionary in python Rupini 3 6,965 May-15-2020, 04:57 PM
Last Post: snippsat
  Read text file, process data and print specific output Happythankyoumoreplease 3 2,902 Feb-20-2020, 12:19 PM
Last Post: jefsummers
  Convert text from an image to a text file Evil_Patrick 5 4,268 Jul-30-2019, 07:57 PM
Last Post: DeaD_EyE
  read from file mcgrim 16 6,093 May-14-2019, 10:31 AM
Last Post: mcgrim
  reading text file and writing to an output file precedded by line numbers kannan 7 10,367 Dec-11-2018, 02:19 PM
Last Post: ichabod801
  Read directly from excel file using python script dvldgs05 0 2,250 Oct-19-2018, 02:51 AM
Last Post: dvldgs05
  Read a data from text file (Notepad) Leonzxd 24 13,839 May-23-2018, 12:17 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020