循环直到在Python中收到特定的用户输入 [英] Loop until a specific user input is received in Python

查看:397
本文介绍了循环直到在Python中收到特定的用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在对学校进行评估.我是python的新手,我真的不知道如何循环使用它

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

推荐答案

要回答您的问题,您需要循环直到给出给定的输入. 因此,您将使用:

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

这篇关于循环直到在Python中收到特定的用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆