循环,直到在Python中收到特定的用户输入

时间:2016-08-23 08:45:08

标签: python loops

您好我正在为学校做评估。我是python的新手,我真的不知道如何循环这个

    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

我试图做到这一点,以便它会询问用户他们是否想在完成一个学生后输入更多的学生数据。例如,如果他们希望继续并且基本上再次重复编码。我真的很喜欢一些帮助

3 个答案:

答案 0 :(得分:3)

要回答你的问题,你需要的是循环,直到给定给定的输入。 所以你会使用:

&&

答案 1 :(得分:0)

你只需循环它

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

现在它有效。只记得输入中的引号。或者解决它。

答案 2 :(得分:0)

我遇到了相同的问题,并设法以更简单的方式解决了该问题,而不必将任何代码更改太多,例如,假设您希望在用户输入“是”之前询问一个问题:

("6","7")

如果您具有清除功能并在询问问题之前清除屏幕,则该效果最佳,因此不会堆积。 如果此方法不起作用,请摆脱中断,然后执行nextcode()