1

我一直试图让这个程序工作,它运行,但一旦我让它运行,我发现了这个问题。现在它要求输入成绩点和学分,并按预期打印出 GPA,但我可以在成绩点中输入任何内容,它会输出一个值。我想要的是仅当用户输入介于 0.0-4.0 之间的数字时,gradePoint 才能工作。我在考虑某种循环,比如 if 循环,但有没有更简单的方法(或首选方法)来做到这一点?

这是代码:

def stu():
stu = Student

class Student:
def __init__(self, name, hours, qpoints):
    self.name = name
    self.hours = float(hours)
    self.qpoints = float(qpoints)

def getName(self):
    return self.name

def getHours(self):
    return self.hours

def getQpoints(self):
    return self.qpoints

def getStudent(infoStr):
    name, hours, qpoints = infoStr.split("\t")
    return Student(name, hours, qpoints)

def addGrade(self, gradePoint, credits):
    self.qpoints = gradePoint * credits
    self.hours = credits
    self.gradePoint = float(gradePoint)
    self.credits = float(credits)

def gpa(self):
    return self.qpoints/self.hours

def main():
stu = Student(" ", 0.0, 0.0)
credits = int(input("Enter Credit Hours "))
gradePoint = float(input("Enter Grade Point"))
if gradePoint > 4.0:
        print("Please enter a Grade Point that is less then 4.")
stu.addGrade(gradePoint,credits)
output = stu.gpa()
print("\nStudent GPA is: ", output)


main()

这是外壳打印输出(我知道 GPA 中重复了成​​绩点,这是针对一门课的,我们应该使用框架程序并更改一些东西):

Enter Credit Hours 4
Enter Grade Point 3.5
Student GPA is:  3.5

Enter Credit Hours 12
Enter Grade Point 3.2
Student GPA is:  3.2

谢谢!

编辑:我只是按照评论中的建议做了一个 if 循环,它有点工作,因为它打印出我想要的东西,但没有让它停止并等待正确的输入。我不完全确定如何让它做到这一点。

这是来自外壳的新打印:

Enter Credit Hours 4
Enter Grade Point 3.5
Student GPA is:  3.5

Enter Credit Hours 4
Enter Grade Point 4.5
Please enter a Grade Point that is less then 4.
Student GPA is:  4.5
4

2 回答 2

1

您可以检查用户输入是否符合您的要求,如果不符合则抛出异常。这将使程序记录错误消息并在无效的用户输入时停止。

最基本的方法是使用断言语句:

def main():
    stu = Student(" ", 0.0, 0.0)
    credits = int(input("Enter Credit Hours "))
    assert credits > 0 and credits < 4, "Please enter a number of Credit Hours between 0 and 4."

如果您希望应用程序在输入有效输入之前一直提示用户,则至少需要一个while循环。

举个例子:

def main():
    stu = Student(" ", 0.0, 0.0)
    while True:
        credits = int(input("Enter Credit Hours "))
        if credits > 0 and credits < 4:
            break
        else:
            print("Please enter a number of Credit Hours between 0 and 4")

    while True:
        gradePoint = float(input("Enter Grade Point"))
        if gradePoint <= 4.0:
            break
        else:
            print("Please enter a Grade Point that is less then 4.")

    stu.addGrade(gradePoint,credits)
    output = stu.gpa()
    print("\nStudent GPA is: ", output)
于 2012-04-13T23:38:18.193 回答
0

创建一个函数,从用户那里收集一个范围内的有效浮点数:

def getValidFloat(prompt, error_prompt, minVal, maxVal):
    while True:
        userInput = raw_input(prompt)
        try:
            userInput = float(userInput)
            if minVal <= userInput <= maxVal:
                return userInput
            else: raise ValueError
        except ValueError:
            print error_prompt

然后像这样使用:

gpa = getValidFloat(
    "Enter Grade Point ",
    "You must enter a valid number between 1.0 and 4.0",
    1,
    4)

输出看起来像这样:

Enter Grade Point 6.3
You must enter a valid number between 1.0 and 4.0
Enter Grade Point 3.8
于 2012-04-14T02:45:25.253 回答