-7
votes

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 grade input

merit=70

excellence=85

max=100
import re #imports re an external modeule which defines re
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")
    import re
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")
    import sys
    raise SystemExit
    print(studentfname)
elif len(studentlname)>30:
    print ("Very long string") 
    raise SystemExit
    import re
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")
    import sys
    raise SystemExit
    print(teacherfname)
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")
    import sys
    raise SystemExit
    print(teacherlname)
elif len(teacherlname)>30:
    print ("Very long string") 
    raise SystemExit
    print(teachercode)
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")
    import sys
    raise SystemExit
    print(teachercode)
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))
    break
 except ValueError: 

  continue
#prints if not a number stops letters
if grade >merit>excellence>= achieved: 
 print("Achieved")
if grade < achieved:
    print("not achieved")
if grade >=merit>excellence < excellence:
    print("merit")
if grade >= excellence > merit:
    print("excellence")
if grade < 0:
    print("can't be negative")
    raise SystemExit
if grade > max:
   print("Cannot be more than 100")
   raise SystemExit
print("student's details")#last print of variablesa
print(studentfname,studentlname)
print("teacher's details")
print(teacherfname,teacherlname,teachercode)
print("student's grade")
print(grade)
if grade >merit>excellence>= achieved: 
 print("Achieved")
if grade < achieved:
    print("not achieved")
if grade >=merit>excellence < excellence:
    print("merit")
if grade >= excellence > merit:
    print("excellence")
if grade < 0:
    print("can't be negative")
    raise SystemExit
if grade > max:
   print("Cannot be more than 100")
   raise SystemExit
print("Thanks for adding in the grades")

        break

Im trying to make it so that it will ask the user if they would like to input more student data after they have done one student. eg like if they wish to continue and basically repeat the coding again. I would really love some help

3
You should read this: stackoverflow.com/help/mcve. We need a minimal working code to help you - LBes
Put the whole thing in a while True: loop. Then if input('enter more?')=='no': break. - Aran-Fey

3 Answers

4
votes

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
votes

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
votes

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()