Asking a user for a correct input until they give a correct response

Click For Summary
SUMMARY

The discussion focuses on creating a Python program that prompts users for their name and grade, ensuring valid input. The program must repeatedly ask for the name until it contains no digits and for the grade until it is 10 or lower. Key suggestions include using a while loop for continuous prompting and a for loop to check each character in the name. The final structure should utilize logical conditions to manage input validation effectively.

PREREQUISITES
  • Basic understanding of Python programming
  • Familiarity with loops in Python (while and for loops)
  • Knowledge of string manipulation and character checking in Python
  • Understanding of conditional statements in Python
NEXT STEPS
  • Learn about Python input validation techniques
  • Explore Python's string methods for character checking
  • Study the use of logical operators in Python
  • Investigate best practices for user input handling in Python applications
USEFUL FOR

Beginner Python developers, educators teaching programming concepts, and anyone interested in improving user input validation in Python applications.

acurate
Messages
17
Reaction score
1

Homework Statement


I need a program (in PYTHON), which would ask a user his name and grade, then print them both. 1) If the name has a number it, the program has to ask the same question again until the name is entered correctly 2) The same with the grade. If it's higher than 10, the program has to keep on asking the same request.

Homework Equations

The Attempt at a Solution


Python:
while True:
    name = input("Enter a student's name:" )
    for digit in name:
        if digit.isdigit():
            #don't know what to do here and how to continue
         
    grade = input("Enter a grade: ")
    if grade > 10:
        # i need the program to do the same thing as in the name
        # if the grade is higher than 10 the program needs to keep loopong the input
    print (name, grade)
 
Last edited by a moderator:
Physics news on Phys.org
What language is this?

There may be a couple more structures you might need.
 
DaveC426913 said:
What language is this?

There may be a couple more structures you might need.
Oh! I completely forgot this is Python! Thank you for noticing. Geez, I'm clumsy.
 
acurate said:
Oh! I completely forgot this is Python! Thank you for noticing. Geez, I'm clumsy.
Sorry. I notice that you did actually mention Python.
 
DaveC426913 said:
Sorry. I notice that you did actually mention Python.
No, no I added (in PYTHON) right when you mentioned it. :D It's okay.
 
  • Like
Likes   Reactions: DaveC426913
What you want to do is exit the loop if BOTH tests return false.But - it might read better if you did it the other way around.

validName = false
validGrade = false
while (NOT validName) OR (NOT validGrade):
do tests
prompt for input

This, way your tests give you false until you get valid input. It's functionally identical to your way, but conceptually closer to the intent. And may help you think through it easier.Is there a way in Python of performing logical/binary compares? i.e. logical AND ?
 
Last edited:
acurate said:

Homework Statement


I need a program (in PYTHON), which would ask a user his name and grade, then print them both. 1) If the name has a number it, the program has to ask the same question again until the name is entered correctly 2) The same with the grade. If it's higher than 10, the program has to keep on asking the same request.

Homework Equations

The Attempt at a Solution


Code:
while True:
    name = input("Enter a student's name:" )
    for digit in name:
        if digit.isdigit():
            #don't know what to do here and how to continue
         
    grade = input("Enter a grade: ")
    if grade > 10:
        # i need the program to do the same thing as in the name
        # if the grade is higher than 10 the program needs to keep loopong the input
    print (name, grade)
Your code for getting the user's name should look something like this:
Code:
while True:
   name = input("Enter a student's name:" )
   for ch in name:
      if ch.isdigit():
         continue
   break
Notice that I changed the name of one of your variables from digit to ch, since calling it "digit" seems misleading to me. The string consists of characters, which could be alphabetic or numeric or some other type (like punctuation).
The idea is to iterate through the string character by character. If a digit is encountered, the while loop starts up again, asking for the user's name. If the for loop does not encounter a digit character, the break statement causes the while loop to be exited.
 
Mark44 said:
Your code for getting the user's name should look something like this:
Code:
while True:
   name = input("Enter a student's name:" )
   for ch in name:
      if ch.isdigit():
         continue
   break
Notice that I changed the name of one of your variables from digit to ch, since calling it "digit" seems misleading to me. The string consists of characters, which could be alphabetic or numeric or some other type (like punctuation).
The idea is to iterate through the string character by character. If a digit is encountered, the while loop starts up again, asking for the user's name. If the for loop does not encounter a digit character, the break statement causes the while loop to be exited.
Thank you for your reply. I just have one question, do I need to use another loop for starting the grade input or use the same while?
 
acurate said:
Thank you for your reply. I just have one question, do I need to use another loop for starting the grade input or use the same while?
It should be another loop. It should continue iterating until the input number is acceptable.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
Replies
7
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 6 ·
Replies
6
Views
1K
  • · Replies 1 ·
Replies
1
Views
7K
Replies
9
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K