0

I'm having problems. The code works, but I want it to work without appending the final input from user. Not sure how to end the loop without putting the incorrect variable in the list at the end. Quizzes = []

#Create variable to start the counter in the while loop
QuizNumber = 1

#Create variable to start while loop
QuizValue = 0

#Create while loop that ends if the user types anything outside of 0 and 15
while int(QuizValue) >= 0 and int(QuizValue) <= 15:

    #Get user input for quiz values
    QuizValue = input("Quiz " +str(QuizNumber) + ":")



    #Make if statement to end while loop if user types anything not integer
    if int(QuizValue) >= 0 or int(QuizValue) <= 15:
        QuizValue = QuizValue

        #Append to list
        Quizzes.append(QuizValue)

    else:
        QuizValue = 999


    #Counter for quiz number
    QuizNumber = QuizNumber + 1
TomPJacks
  • 1
  • 1

4 Answers4

0

Try Quizzes = Quizzes[:-1] after the loop

This splices the list and gives you a new list which has all except the last item

gokul_uf
  • 740
  • 1
  • 8
  • 21
  • `Quizzes.pop()` would probably be a little more preferable because it's more performant (doesn't make a whole new list). – Trey Hunner Feb 23 '16 at 04:22
0

A common Python idiom is to use while 1: to begin an "infinite" loop, but then to have a break statement in the middle of the loop when a certain condition is met. This takes the place of the do ... until form found in other languages. So:

while 1:  # an "infinite" loop
    QuizValue = input("Quiz " +str(QuizNumber) + ":")
    # Do something if QuizValue is between 0 and 15
    if 0 <= int(QuizValue) <= 15:
        Quizzes.append(QuizValue)
    else:
        break

By the way, in your code, this:

if int(QuizValue) >= 0 or int(QuizValue) <= 15:

...will always be true. Every single number is either greater than zero or less than 15.

jgfооt
  • 914
  • 6
  • 12
0

pop

You could use pop to remove the last item:

QuizNumber = 1
QuizValue = 0
while 0 <= int(QuizValue) <= 15:
    QuizValue = input("Quiz " +str(QuizNumber) + ":")
    Quizzes.append(QuizValue)
    QuizNumber = QuizNumber + 1
Quizzes.pop()

That way you wouldn't need an if statement at all.

Also note that I rewrote that 0 <= x and x <= 15 conditional to 0 <= x <= 15 which is the same thing in Python.

infinite loop

If you'd like an if statement, you could actually loop infinitely with while True and then use break. This way you don't need to write the same conditional check twice:

QuizNumber = 1
QuizValue = 0
while True:
    QuizValue = input("Quiz " +str(QuizNumber) + ":")
    if not (0 <= int(QuizValue) <= 15):
        break
    Quizzes.append(QuizValue)
    QuizNumber = QuizNumber + 1

Again note that x < 0 or x > 15 is the same as not (0 <= x <= 15)

A couple unrelated code suggestions

You might want to try using the format method on strings instead of using string concatenation. This will allow you to turn:

    quiz_value = input("Quiz " +str(quiz_number) + ":")

Into:

    quiz_value = input("Quiz {n}:".format(n=quiz_number))

You also may want to use underscore_case in your variable names instead of CamelCase (underscore case is more idiomatic in Python).

quiz_number = 1
quiz_value = 0
while 0 <= int(quiz_value) <= 15:
    quiz_value = input("Quiz {n}:".format(n=quiz_number))
    quizzes.append(quiz_value)
    quiz_number = quiz_number + 1
quizzes.pop()
Trey Hunner
  • 10,975
  • 4
  • 55
  • 114
  • Thanks! Do you happen to know any way to end the loop by pressing enter? Right now it's giving me "ValueError: invalid literal for int() with base 10: ' ' " – TomPJacks Feb 23 '16 at 04:30
  • When you convert any arbitrary string to an integer, it may fail by raising an exception. You're going to want to catch that exception. In your case I would remove the `int(quiz_value)` and instead put your `input()` call in an `int()` call like so: `quiz_value = int(input("Quiz {n}:".format(n=quiz_number)))`. Then you'll just need to wrap that line in a try-except to handle the exception (see here: http://stackoverflow.com/a/8075959/98187). – Trey Hunner Feb 23 '16 at 04:33
  • Ok. Thanks. I will try that. – TomPJacks Feb 23 '16 at 04:35
0

Always use try, except clause when dealing with unexpected input from user. Following code uses a boolean Flag to break while loop and avoids else condition.

QuizNumber = 1
QuizValue = 0
Quizzes = []
flag = True

while flag:
    try:
        QuizValue = input("Quiz " +str(QuizNumber) + ":")
        flag = False
        if str(QuizValue).isdigit():
            if int(QuizValue) >= 0 and int(QuizValue) <= 15:
                Quizzes.append(QuizValue)
                QuizNumber = QuizNumber + 1
                flag = True
    except Exception as e:
        flag = False
hemraj
  • 964
  • 6
  • 14