1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How to continue this Python program?

  1. Mar 21, 2015 #1
    1. The problem statement, all variables and given/known data
    I need a program where the program needs to ask me a name of a student and then a grade.
    1) The program needs to end if a 0 is entered.
    2) If the name has a number in it, the program should ask the user to enter another name.
    3) After the name is correctly entered the program needs to ask to enter the student's grade
    4) Student's grade cannot be higher than 10 but no less than 2.
    5) Also the program should end if a 0 is entered in the grade area

    2. Relevant equations


    3. The attempt at a solution
    This is what I've done:

    Code (Text):

    while True:
        name = input ("Enter a student's name: ")
        if name.isalpha():
            continue
        elif name == "0":
            name = input ("A zero is entered. Session cancelled.")
            break
        else:
            while not name.isalpha():
                name = input ("Error. Enter a student's name again: ")
     
    How do I make the program ask for the student's grade AFTER the grade is entered correctly?
    Because what I have now it only asks me to enter a grade ONLY AFTER the grade is entered incorrectly and entered again.

    How can I get the program to ask the grade right after the name is entered correctly after the first try?
    I'm sure there is something wrong with the continue's and break's but I cannot tell what.

    Should I continue like this:

    Code (Text):

    while True:
        name = input ("Enter a student's name: ")
        if name.isalpha():
            continue
        elif name == "0":
            name = input ("A zero is entered. Session cancelled.")
            break
        else:
            while not name.isalpha():
                name = input ("Error. Enter a student's name again: ")
    while True:
        grade = input ("Enter a student's grade: ")
        if grade >= 10:
            continue
        elif grade == "0":
            grade: input ("A zero is entered. Session cancelled.")
            break
        else:
            while not grade ?
     
  2. jcsd
  3. Mar 21, 2015 #2

    jedishrfu

    Staff: Mentor

    Organizationally you can make a method for each question and in the method place your input query and validation surrounded by a while loop that exits when the answer is validated.
     
  4. Mar 22, 2015 #3

    NascentOxygen

    User Avatar

    Staff: Mentor

    @acurate Are you studying Python in school, or are you learning it as self-study?

    It's a while since I've used Python, but I expect it won't like code like this:

    grade = input ("Enter a student's grade: ")
    if grade >= 10:

    You are reading input as a string, but comparing it numerically and with an integer.

    I'll give you the best advice you'll ever get about writing code. Always, always, always have a sample program listing alongside as a guide so that you can refer to similar constructs that you are needing. This practice alone will eliminate 90% of the simple mistakes and oversights to which you will otherwise be prone. Code for the type of program you are working on has been printed in every "Introduction to XXXX Programming" textbook printed since Caxton invented his printing press! Python instruction material is especially good at this because its proponents are earnest in trying to convince readers of the ease and simplicity of Python's power. :wink: There is no excuse for you to be sweating over sequencing conditional statements, and wondering how to compare alpha strings with a range of integers. It has all been written out for you. Just get hold of an introductory textbook or google an online tutorial. You will find the sort of code you need within the first few chapters. If you try to code without modelling your statements on working code right at hand you'll find the experience drudgery and exasperating, when it should be engrossing and exhilarating! Learning programming cannot be compared to learning any other subject, you need to adopt new learning strategies to help yourself. Programming is an art; a good programmer is an artisan; there is an art to learning programming!

    Get thee to a textbook or online tutorial without delay! I know that in one of the PF forums there is a sticky thread listing some recommended Python resources.

    P.S. Because you included that fixed format code, my tablet is restricting me to a 30 char x 2 line window in which to type this.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: How to continue this Python program?
Loading...