-7

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

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Mr Sandman
  • 15
  • 1
  • 3
  • 1
    You should read this: http://stackoverflow.com/help/mcve. We need a minimal working code to help you – LBes Aug 23 '16 at 08:47
  • Put the whole thing in a `while True:` loop. Then `if input('enter more?')=='no': break`. – Aran-Fey Aug 23 '16 at 08:47

3 Answers3

4

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
LBes
  • 3,366
  • 1
  • 32
  • 66
0

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.

Evus
  • 400
  • 4
  • 12
  • Hey. Thanks I have tried the code that you gave. but it doesn't display any of the outputs for example before it would state the students name teachers name and what grade they got but now its just skipping all of that and asks for more. Could you please help thanks :) – Mr Sandman Aug 23 '16 at 09:05
  • Ok, try now again. – Evus Aug 23 '16 at 09:10
  • hey I tried it again and its doing this Input Student first name in lowercase james james Input Student last name in lowercase smith smith Input your first name in lowercase jerry jerry Input your last name in lowercase cake cake Input your teacher code in lowercase nl nl Enter student's grade 89 89 More? when its meant to be something like this – Mr Sandman Aug 23 '16 at 09:13
  • Input Student first name in lowercase larry larry Input Student last name in lowercase the Input your first name in lowercase king Input your last name in lowercase pin Input your teacher code in lowercase nl Enter student's grade 89 89 excellence student's details larry the teacher's details king pin nl student's grade 89 excellence Thanks for adding in the grades => None – Mr Sandman Aug 23 '16 at 09:13
  • so after all the inputs it displays everything like that. Really sorry for distributing you this much but thanks – Mr Sandman Aug 23 '16 at 09:15
  • Hey thanks its kind of fixed right now. Im really sorry that it breaks ur heart but I utterly most appreciate what you are doing as I am quite the newb and I cant thank you enough. Just a minor error in that it is repeating it twice Enter student's grade 89 89 excellence student's details james smith teacher's details kato kie pi student's grade 89 excellence Student james smith Teacher kato kie pi Grade 89 excellence Thanks for adding in the grades More? Thanks again – Mr Sandman Aug 23 '16 at 09:40
  • OḲ. Removed doubles. If you want to change how it prints just edit lines after "print("Student")" We are kind of offtopic now. – Evus Aug 23 '16 at 09:48
0

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

H Gaming
  • 58
  • 8