How to continue this Python program?

  • Context: Python 
  • Thread starter Thread starter acurate
  • Start date Start date
  • Tags Tags
    Error Program Python
Click For Summary
SUMMARY

The discussion focuses on creating a Python program that prompts users for a student's name and grade, with specific validation rules. The program should terminate if "0" is entered, ensure names contain only alphabetic characters, and enforce grade limits between 2 and 10. The user is advised to separate input validation into distinct methods to improve code clarity and functionality. Additionally, it is emphasized that input should be converted from string to integer for proper numerical comparisons.

PREREQUISITES
  • Understanding of Python input handling
  • Familiarity with conditional statements in Python
  • Knowledge of data type conversion in Python
  • Basic programming concepts such as loops and functions
NEXT STEPS
  • Learn about Python functions to modularize code
  • Study Python's input() function and data type conversion
  • Explore error handling in Python to manage user input
  • Review Python tutorials focusing on input validation techniques
USEFUL FOR

Beginner Python programmers, educators developing student assessment tools, and anyone interested in improving their input validation skills in Python.

acurate
Messages
17
Reaction score
1

Homework Statement


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

Homework Equations

The Attempt at a Solution


This is what I've done:

Code:
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:
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 ?
 
Technology news on Phys.org
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.
 
@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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K