How to continue this Python program?

In summary: This is a real pain. I have to use a stylus, and can't zoom the display. :frown:In summary, the conversation discusses the need for a program that asks for a student's name and grade, with specific criteria for ending the program and validating the inputs. The speaker is struggling with sequencing and comparing inputs and is advised to refer to sample code and resources to improve their learning and coding experience.
  • #1
acurate
17
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
  • #2
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.
 
  • #3
@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.
 

1. How do I save my work and continue the program later?

To save your work in Python, you can use the built-in function "pickle" to serialize your data and save it to a file. Then, when you want to continue your program, you can use the "unpickle" function to load your saved data back into your program.

2. What is the best way to organize my code for easy continuation?

One way to organize your code for easy continuation is to break it into smaller, modular functions. This allows you to easily pick up where you left off and make changes without affecting the entire program. Additionally, commenting your code and using meaningful variable names can also help with organization.

3. Can I continue a Python program on a different computer?

Yes, as long as you have the necessary files and libraries, you can continue a Python program on a different computer. It is important to make sure that the Python versions and dependencies are compatible between the two computers.

4. How can I troubleshoot errors when trying to continue my Python program?

If you encounter errors when trying to continue your Python program, the first step is to carefully read the error message and try to understand what the issue is. You can also use debugging tools such as print statements or a debugger to help identify the source of the error. Additionally, searching for solutions online or asking for help from more experienced programmers can also be helpful in troubleshooting errors.

5. Is it possible to continue a Python program after it has been terminated or crashed?

In most cases, if a Python program has been terminated or crashed, it cannot be continued from where it left off. However, you can use the traceback information to identify the cause of the crash and make necessary changes to prevent it from happening again in the future.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
18
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
18
Views
2K
  • Programming and Computer Science
2
Replies
36
Views
3K
Back
Top