【问题标题】:Loop until a specific user input is received in Python [duplicate]循环直到在Python中接收到特定的用户输入[重复]
【发布时间】:2019-09-26 18:07:24
【问题描述】:

您好,我正在为学校做评估。我是 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

我正在尝试让它在完成一名学生后询问用户是否愿意输入更多学生数据。例如,如果他们希望继续并基本上再次重复编码。我真的很想得到一些帮助

【问题讨论】:

  • 您应该阅读以下内容:stackoverflow.com/help/mcve。我们需要一个最小的工作代码来帮助你
  • 把整个东西放在一个while True:循环中。然后if input('enter more?')=='no': break.

标签: python loops


【解决方案1】:

我遇到了同样的问题并设法以一种更简单的方式修复它,而不必对任何代码进行太多更改,例如,假设您希望在用户输入“是”之前提出问题:

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

如果您有一个清晰的功能并在提问之前清除屏幕,这样它就不会堆积起来,这样效果最好。 如果这不起作用,请摆脱 break 并执行 nextcode()

【讨论】:

    【解决方案2】:

    你只是循环它

    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
    

    现在可以了。只需记住输入中的引号即可。或者修复它。

    【讨论】:

    • 嘿。谢谢,我已经尝试了您提供的代码。但它不显示任何输出,例如,它会说明学生姓名教师姓名和他们得到的成绩,但现在它只是跳过所有这些并要求更多。你能帮忙吗谢谢:)
    • 好的,现在再试一次。
    • 嘿,我又试了一次,它正在这样做 输入小写学生名字 james james 输入小写学生姓 smith smith 用小写输入你的名字 jerry jerry 用小写输入你的姓 cake cake 输入你的老师代码小写 nl nl 输入学生的年级 89 89 更多?当它应该是这样的时候
    • 以小写形式输入学生名字 larry larry 以小写形式输入学生姓氏 以小写形式输入您的名字 king 以小写形式输入您的名字 pin 以小写形式输入您的老师代码 nl 输入学生的成绩 89 89 优秀学生的详细信息 larry 老师的详细信息 king pin nl 学生的成绩 89 优秀 感谢您添加成绩 => 无
    • 所以在所有输入之后,它会显示所有类似的内容。真的很抱歉分发你这么多,但谢谢
    【解决方案3】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 2011-12-28
      • 1970-01-01
      • 2021-05-17
      • 2018-09-13
      • 2021-04-19
      • 1970-01-01
      • 2017-04-18
      • 2022-12-31
      相关资源
      最近更新 更多