Home > Mobile >  Why does my while loop repeat infinitely in Python 3?
Why does my while loop repeat infinitely in Python 3?

Time:03-29

Why does my while loop repeat infinitely in Python 3? I'm trying to create a revamped riddle program and have run into 2 issues. My first is the while loop is running infinitely, and whatever the selected riddle is it's infinitely repeating. My program is supposed to create an empty list, and put 5 random numbers in it, then take that list and check if the length of it is less than 5. If it is the main program runs. I've already tried to take the integer version of the length of the list. (I'm new to programming and 12 so the answer is probably simple.) Here's my code :

import sys, random
print('Welcome to Random Riddles!')
print('This is a list of hard randomized riddles!')
print('Please type either (q)uit, or (s)tart to start this quiz or after each riddle')
Choices = input()

randRiddle_list = []
randomRiddle = random.randint(1,5)
for i in range(5) :
   randomRiddle = random.randint(1,5)
   randRiddle_list.append(randomRiddle)

while int(len(randRiddle_list)) <= 5 :
if randomRiddle == 1 :
    if Choices == 's' :
        print('Okay then, what is so fragile that saying its name breaks it?')
        print('A: Silence, B: Light, C: Clothes, D: The Dark One')
    elif Choices == 'q' :
        sys.exit()

CodePudding user response:

The first, there's only going to be five identical numbers in this randRiddle_list, try this code

randRiddle_list = []
for i in range(5) :
   randRiddle_list.append(random.randint(1,5))

And then, the length of randRiddle_list will always be five, so while int(len(randRiddle_list)) <= 5 : is running infinitely. Finally, if randomRiddle is not 1, the code if randomRiddle == 1 : will never be True

CodePudding user response:

When you reach the following statement:

int(len(randRiddle_list)) <= 5

int(len(randRiddle_list)) is to the length of randRiddle_list. Each time the while loop re-evaluates this, the length is the same. So you need to edit the length of randRiddle_list with some command (del or pop) in order for the length to be <=5.

I think your main mistake is thinking that the while loop with your riddles is executing before the riddle list is full. What's happening is:

# After the user has entered a value, choices equals that value, and won't change
Choices = input()

randRiddle_list = []
# randomRiddle will equal one random number [1,5]
randomRiddle = random.randint(1,5)
for i in range(5) :
    # You are adding that same random number to randRiddle_list 5 times
   randRiddle_list.append(randomRiddle)

The code you provided doesn't ever change the length of randRiddle_list, because it's never edited after this point. Python doesn't go back here for the rest of the script.


Perhaps you would like to go over each riddle?

import sys
import random
print('Welcome to Random Riddles!')
print('This is a list of hard randomized riddles!')
print('Please type either (q)uit, or (s)tart to start this quiz or after each riddle')
Choices = input(">")
randRiddle_list = []

# This here is important, you add 5 random numbers.
# As random.randint(1, 5) gives a new random number each iteration of the loop
for i in range(5):
    # Adds a random number 5 times
    randRiddle_list.append(random.randint(1, 5))

for riddle in randRiddle_list:
    if riddle == 1:
        if Choices == 's':
            print('Okay then, what is so fragile that saying its name breaks it?')
            print('A: Silence, B: Light, C: Clothes, D: The Dark One')
            # Ask the user for their input once again
            Choice_riddle = input(">")
            if Choice_riddle == "A":
                print("Correct!")
            else:
                print("Wrong :(")

        elif Choices == 'q':
            sys.exit()
    else:
        # The random numbers in randRiddle_list are not 1, so if riddle == 1: didn't execute
        print("You did not get riddle 1")
  • Related