1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Mar 17, 2015 #1
    1. The problem statement, all variables and given/known data
    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.

    2. Relevant equations


    3. The attempt at a solution
    Code (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: Mar 17, 2015
  2. jcsd
  3. Mar 17, 2015 #2

    DaveC426913

    User Avatar
    Gold Member

    What language is this?

    There may be a couple more structures you might need.
     
  4. Mar 17, 2015 #3
    Oh! I completely forgot this is Python! Thank you for noticing. Geez, I'm clumsy.
     
  5. Mar 17, 2015 #4

    DaveC426913

    User Avatar
    Gold Member

    Sorry. I notice that you did actually mention Python.
     
  6. Mar 17, 2015 #5
    No, no I added (in PYTHON) right when you mentioned it. :D It's okay.
     
  7. Mar 17, 2015 #6

    DaveC426913

    User Avatar
    Gold Member

    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: Mar 17, 2015
  8. Mar 17, 2015 #7

    Mark44

    Staff: Mentor

    Your code for getting the user's name should look something like this:
    Code (Text):
    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.
     
  9. Mar 17, 2015 #8
    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?
     
  10. Mar 17, 2015 #9

    Mark44

    Staff: Mentor

    It should be another loop. It should continue iterating until the input number is acceptable.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Asking a user for a correct input until they give a correct response
  1. It's correct or not (Replies: 2)

Loading...