Loop until a specific user input is received in Python

后端 未结 3 2106
半阙折子戏
半阙折子戏 2020-11-28 16:25

Hello Im doing a assessment for school. Im quite a newbie to python and I really have no idea on how to loop this

    achieved=50 #predefined variables for g         


        
相关标签:
3条回答
  • 2020-11-28 16:49

    To answer your question what you need is to loop until a given input is given. So you would use:

    while True:    # infinite loop
        user_input = raw_input("Want to continue? ")
        if user_input == "No":
            break  # stops the loop
        else:
            # do whatever computations you need
    
    0 讨论(0)
  • 2020-11-28 16:51

    You just loop it

    achieved=50 #predefined variables for grade input
    
    merit=70
    
    excellence=85
    
    max=100
    import sys
    import re #imports re an external modeule which defines re
    while(1):
        studentfname = input("Input Student first name in lowercase")
        if any( [ i>'z' or i<'a' for i in studentfname]):#checks if is in lowercase letters
            print ("Invalid input must be letter and in lowercase")
            raise SystemExit
        print(studentfname)
        studentlname = input("Input Student last name in lowercase")
        if any( [ i>'z' or i<'a' for i in studentlname]):
            print ("Invalid input must be letter and in lowercase")
            raise SystemExit
        elif len(studentlname)>30:
            print ("Very long string")
            raise SystemExit
        print(studentlname)
        teacherfname = input("Input your first name in lowercase")
        if any( [ i>'z' or i<'a' for i in teacherfname]):
            print ("Invalid input must be letter and in lowercase")
            raise SystemExit
        elif len(teacherfname)>30:
            print ("Very long string")
            raise SystemExit
        print(teacherfname)
        teacherlname = input("Input your last name in lowercase")
        if any( [ i>'z' or i<'a' for i in teacherlname]):
            print ("Invalid input must be letter and in lowercase")
            raise SystemExit
        elif len(teacherlname)>30:
            print ("Very long string")
            raise SystemExit
        print(teacherlname)
        teachercode = input("Input your teacher code in lowercase")
        if any( [ i>'z' or i<'a' for i in teachercode]):
            print ("Invalid input must be letter and in lowercase")
            raise SystemExit
        elif len(teachercode)>30:
            print ("Very long string")
            raise SystemExit
        print(teachercode)
        while True: #inputs student depending on the input prints out results id achieved, merit and excellence
            try:
                grade = int(input("Enter student's grade"))
                print(str(grade))
            except ValueError:
                continue
            if grade >merit>excellence>= achieved:
                graden = "Achieved"
                print("Achieved")
            if grade < achieved:
                print("not achieved")
            if grade >=merit>excellence < excellence:
                graden = "merit"
                print("merit")
            if grade >= excellence > merit:
                graden = "excellence"
                print("excellence")
            if grade < 0:
                print("can't be negative")
                raise SystemExit
            if grade > max:
                print("Cannot be more than 100")
                raise SystemExit
            print(grade)
            print("Student")
            print(studentfname)
            print(studentlname)
            print("Teacher")
            print(teacherfname)
            print(teacherlname)
            print(teachercode)
            print("Grade")
            print(str(grade))
            print(graden)
            print("Thanks for adding in the grades")
            break
        if input('More?')=='no': break
    

    Now it works. Just remember quotation marks in input. Or fix it.

    0 讨论(0)
  • 2020-11-28 16:56

    i have come across the same issue and managed to fix it an easier way while not having to change any of your code to much, for example lets say you want a question to be asked until user inputs "yes":

    print ("Question here")
    Input = input("").lower() 
    if Input == "yes":
        break
        nextcode() //optional
    else:
        while Input != "yes":
        print ("Question here")
        Input = input("").lower()
        if Input == "back":
            break
            nextcode() //optional
    

    this works best if you have a clear function and clear the screen before asking the question so it dose not stack up. if this dose not work get rid of break and just do nextcode()

    0 讨论(0)
提交回复
热议问题