Python Forum
Not able to validate data from a file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Not able to validate data from a file
#1
Hello! What I am trying to do, is the following:
This program opens a file, where it is supposed to read lines of data (Which right now it does fine, in correct order), and validate the name, to ensure that line where the name would be is not empty. However, when I try to validate it, it essentially ignores my validation and proceeds like it's not even there. How can I get it to work?
Thanks in advance!
def welcome():
    print("Welcome to Academic Helper 2,0.")
    print("This program determines the grade you would get or the number of hours you need to study")

def doAnOption():
    enterAnOption = input("\nPlease enter A if you would like to find out number of hours you would need to study for the grade you want\n\nPlease enter B if you would like to find out the grade you would get based on number of hours you are studying per week\n")
    if enterAnOption.upper() == "A":
        optionA()
    elif enterAnOption.upper() == "B":
        optionB()

def optionA():
    studHours = open("StudyHours.txt", "r")
    name = str(studHours.readline())
    while name != "":
        validateName(name)
        creditHours = studHours.readline()
        grade = studHours.readline()
        print(name, creditHours, grade,)
        name = studHours.readline()

def validateName(name):
    while name == "":
        name = input("Name can not be empty, please enter proper name")

        
def gradeStudyWeekHours(creditHours):
    classesTaken = int(creditHours) / 3
    print(classesTaken)

def optionB():
    print("something")

welcome()
doAnOption()
Reply
#2
On line 23 where you check if there name == ''. Try putting a space in between the quotes
Reply
#3
(Apr-20-2019, 03:33 AM)SheeppOSU Wrote: On line 23 where you check if there name == ''. Try putting a space in between the quotes
Thanks for your response.
I just tried that, but it still does the same thing. Here's how it is displayed.

Welcome to Academic Helper 2,0.
This program determines the grade you would get or the number of hours you need to study

Please enter A if you would like to find out number of hours you would need to study for the grade you want

Please enter B if you would like to find out the grade you would get based on number of hours you are studying per week
a
- Here it shows blank space, where's in that case, I want it to ask me to input the name in
12
A

Tom brady
9
B

philip Rivers
3
A

Joe Theismann
15
B

401240124
B
D
Reply
#4
Sry for late response but I noticed somethign in optionA - it says while name != '' and then inside the name verification it says while name == ''

I'm testing it right now.

Could you tell me what is in the text document
Reply
#5
(Apr-20-2019, 04:06 AM)SheeppOSU Wrote: Sry for late response but I noticed somethign in optionA - it says while name != '' and then inside the name verification it says while name == ''

I'm testing it right now.

Could you tell me what is in the text document

- This is blank
12
A
Tom brady
9
B
philip Rivers
3
A
Joe Theismann
15
B
401240124
18
D
Reply
#6
Oh sorry
Reply
#7
(Apr-20-2019, 04:36 PM)SheeppOSU Wrote: Oh sorry

I got it working.
I needed to strip \n, and now it works perfectly.

I attached code for the reference.
Thanks for trying!

def welcome():
    print("Welcome to Academic Helper 2,0.")
    print("This program determines the grade you would get or the number of hours you need to study")

def doAnOption():
    enterAnOption = input("\nPlease enter A if you would like to find out number of hours you would need to study for the grade you want\n\nPlease enter B if you would like to find out the grade you would get based on number of hours you are studying per week\n")
    if enterAnOption.upper() == "A":
        optionA()
    elif enterAnOption.upper() == "B":
        optionB()

def optionA():
    studHours = open("StudyHours.txt", "r")
    name = str(studHours.readline())
    while name != "":
        name = validateName(name)
        creditHours = studHours.readline()
        grade = studHours.readline()
        print(name, creditHours, grade,)
        name = studHours.readline()

def validateName(name):
    name = name.rstrip("\n")
    while name == "":
        name = input("Name can not be empty, please enter proper name\n")
        name += "\n"
    return name

        
def gradeStudyWeekHours(creditHours):
    classesTaken = int(creditHours) / 3
    print(classesTaken)

def optionB():
    print("something")

welcome()
doAnOption()
Reply
#8
That's good
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Login Module Help - Comparing data in a text file to data held in a variable KeziaKar 0 2,250 Mar-08-2018, 11:41 AM
Last Post: KeziaKar
  Validate string using Regex rahul_raj1981 2 2,776 Jan-23-2018, 05:05 PM
Last Post: rahul_raj1981

Forum Jump:

User Panel Messages

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