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

Click For Summary

Discussion Overview

The discussion revolves around creating a Python program that prompts a user for their name and grade, ensuring that the inputs meet specific criteria. The program should repeatedly ask for the name until it is free of numeric characters and for the grade until it is 10 or lower. The conversation includes attempts at coding solutions, clarifications on programming logic, and questions about the structure of the loops needed.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant outlines the requirements for the program, specifying the conditions under which the user should be prompted again for their name and grade.
  • Another participant suggests that the loop should exit only when both input tests return false, proposing a different logical structure for the program.
  • A participant questions whether to use a separate loop for the grade input or the same while loop used for the name input.
  • Several participants discuss the need for additional structures in the code and clarify the logic of checking for numeric characters in the name input.
  • One participant notes a potential improvement in variable naming to avoid confusion regarding the type of characters being checked.

Areas of Agreement / Disagreement

Participants generally agree on the need for the program to validate user input but have differing views on the structure of the loops and whether to use separate loops for name and grade inputs. The discussion remains unresolved regarding the optimal coding approach.

Contextual Notes

There are unresolved questions about the implementation details, such as the best way to structure the loops and handle input validation. Some assumptions about the input types and conditions are also present but not fully explored.

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