如何在while循环中使用while循环

时间:2015-10-22 17:00:47

标签: python error-handling

如果有人不输入' y'或者' n'?有人可以把它放进我的代码吗?

redo = False

outstanding = 0
satisfactory = 0
unsatisfactory = 0
mark_Total = 0
value = False
while not redo:
    try:
        mark = input("Enter a mark from 0-100")
        if mark < 0 or mark >= 100:
            raise ValueError("Invalid Input")
        elif mark >= 90:
            outstanding += 1
            mark_Total += 1
            again = raw_input("Do you want to enter another mark? (y/n)")
            if again.lower() == "n":
                redo = True

1 个答案:

答案 0 :(得分:2)

在另一个循环中使用while循环没什么特别的。

    elif mark >= 90:
        outstanding += 1
        mark_Total += 1
        again = 'x'
        while again != 'y' and again != 'n':
            again = raw_input("Do you want to enter another mark? (y/n)").lower()
        if again == "y":
            redo = True