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

In summary, the program should ask the user for their name and grade, and continue asking until valid inputs are given. If the name contains a number, the program will ask for the name again. If the grade is higher than 10, the program will ask for the grade again. Once both inputs are valid, the program will print the name and grade. This can be achieved by using a while loop and an if statement for each input, with a break statement to exit the loop once the input is valid.
  • #1
acurate
17
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
  • #2
What language is this?

There may be a couple more structures you might need.
 
  • #3
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.
 
  • #4
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.
 
  • #5
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
  • #6
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:
  • #7
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.
 
  • #8
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?
 
  • #9
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.
 

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

1. How do I prompt a user for input in my program?

In most programming languages, you can use the built-in function input() to prompt a user for input.

2. How do I validate a user's input?

You can use conditional statements, such as if or while, to check if the user's input meets certain criteria. If the input does not meet the criteria, you can prompt the user to enter a correct response until they do so.

3. What is the best way to handle incorrect user input?

The best way to handle incorrect input is to use a loop that prompts the user to enter a correct response until they do so. This ensures that the program does not continue with invalid input.

4. Can I provide a default value if the user does not enter a response?

Yes, you can use the input() function's optional parameter to specify a default value that will be used if the user does not enter a response.

5. Is there a way to limit the number of attempts for the user to enter a correct response?

Yes, you can use a loop with a counter variable to keep track of the number of attempts and break out of the loop if the user exceeds the limit. You can also use a try...except block to catch any errors and prompt the user to try again.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
23
Views
2K
  • Programming and Computer Science
Replies
7
Views
619
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
4K
  • Programming and Computer Science
Replies
18
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top