Python How to continue this Python program?

AI Thread Summary
The discussion centers around creating a Python program that prompts for a student's name and grade, with specific validation rules. Key requirements include terminating the program when "0" is entered, ensuring names do not contain numbers, and enforcing grade limits between 2 and 10. The user is seeking guidance on how to structure the program so that it prompts for the grade immediately after a valid name is entered, rather than waiting for an incorrect input.Participants highlight the importance of input validation and suggest creating separate methods for each input query to streamline the process. There's a critical note on the need to convert input strings to integers for numerical comparisons, as the current code attempts to compare strings with integers, which leads to errors. The advice emphasizes the value of referencing sample code and utilizing programming resources, such as textbooks or online tutorials, to enhance learning and avoid common pitfalls in coding.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top