Python Forum

Full Version: Not able to validate data from a file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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()
On line 23 where you check if there name == ''. Try putting a space in between the quotes
(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
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
(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
Oh sorry
(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()
That's good