![]() |
Cannot read from text file - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Cannot read from text file (/thread-5465.html) |
Cannot read from text file - aljonesy - Oct-05-2017 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" ![]() RE: Cannot read from text file - nilamo - Oct-05-2017 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' RE: Cannot read from text file - aljonesy - Oct-05-2017 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: ") RE: Cannot read from text file - nilamo - Oct-05-2017 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. RE: Cannot read from text file - aljonesy - Oct-05-2017 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 RE: Cannot read from text file - nilamo - Oct-05-2017 Exactly the same reason as before. https://python-forum.io/Thread-Cannot-read-from-text-file?pid=27112#pid27112 |