如何在Python中创建无限的输入?

2024-10-17 10:26:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我应该写一个程序来确定字母等级(a、B、C、D、F),跟踪通过和不通过的学生人数,并显示班级平均成绩。让我感到困惑的一点是“该程序将能够处理用户在本课程中指示的任意数量的学生。”如何创建无限的输入-用户想要多少就有多少

我基本上有一个应该做什么的框架,但我一直在思考如何创建用户想要的尽可能多的输入,然后在其他函数中使用这些信息(如何将所有这些信息输入到另一个函数中)

如果你们中有人能告诉我如何创建无限数量的输入,我将不胜感激!!祝你们今天愉快,伙计们!:)

我的代码:

studentScore = input("Grade for a student: ")

fail = 0
def determineGrade (studentScore):
    if studentScore <= 40:
        print 'F'
    elif studentScore <= 50:
        print 'D'
    elif studentScore <= 60:
        print 'C'
    elif studentScore <= 70:
        print 'B'
    elif studentScore <= 100:
        print 'A'
    else:
        print 'Invalid'

def determinePass (studentScore):
    for i in range():
        if studentScore <= 40:
            fail += 1
        else:
            Pass += 1

def classAverage (studentScore):
    

determineGrade(studentScore)
determinePass(studentScore)

Tags: 函数用户程序信息for数量ifdef
3条回答

无限输入可以使用while loop完成。您可以将该输入保存到其他数据结构(如列表)中,但也可以将代码放在下面

while True:
    x = input('Enter something')
    determineGrade(x)
    determinePass(x)

试试这个

while True:
    try:
        variable = int(input("Enter your input"))
         # your code

    except (EOFError,ValueError):
        break

EOFError-如果从文件中获取输入,则会出现此错误

值错误-以防提供错误输入

要无限次地请求数据,您需要一个while循环

scores=[]    
while True:
    score=input("Students score >>")
    #asks for an input
    if score in ("","q","quit","e","end","exit"):
        #if the input was any of these strings, stop asking for input.
        break
    elif score.isdigit():
        #if the input was a number, add it to the list.
        scores.append(int(score))
    else:
        #the user typed in nonsense, probably a typo, ask them to try again 
        print("invalid score, please try again or press enter to end list")
#you now have an array scores to process as you see fit.

相关问题 更多 >