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

AI Thread Summary
The discussion focuses on creating a Python program that repeatedly prompts the user for their name and grade until valid inputs are provided. The name must not contain any digits, while the grade must be 10 or lower. Participants suggest using a while loop to check for valid inputs, with a break statement to exit the loop when conditions are met. It is recommended to use separate loops for validating the name and grade inputs for clarity. The conversation emphasizes the importance of logical comparisons in Python to achieve the desired functionality.
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 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
6
Views
4K
Replies
6
Views
1K
Replies
1
Views
7K
Replies
10
Views
3K
Replies
2
Views
2K
Replies
23
Views
3K
Back
Top