#๐Ÿ”’ Plz Detect Hidden Bug

83 messages ยท Page 1 of 1 (latest)

craggy steeple
#

What do i wanna achieve:

# Prompts the user for a level,
# If the user does not input 1, 2, or 3, the program should prompt again.
# Randomly generates ten (10) math problems formatted as X + Y = , wherein each of X and Y is a non-negative integer with
#     digits. No need to support operations other than addition (+).
# Prompts the user to solve each of those problems. If an answer is not correct (or not even a number), the program should output EEE and prompt the user again, allowing the user up to three tries in total for that problem. If the user has still not answered correctly after three tries, the program should output the correct answer.
# The program should ultimately output the userโ€™s score: the number of correct answers out of 10.
# Structure your program as follows:
#    wherein get_level prompts (and, if need be, re-prompts) the user for a level and returns 1, 2, or 3, and
#    generate_integer returns a randomly generated non-negative integer with level digits or
#    raises a ValueError if level is not 1, 2, or 3:

my program:

import random

level = 0
while True:
    while (level != 1) and (level != 2) and (level != 3):
        level = int(input("Level: "))

        if (level == 1) or (level == 2) or (level == 3):
            break

    i=0
    a=0
    b=0
    prob = []
    while i != 10:

        a = random.randint(1,100)
        prob.append(a)

        b = random.randint(1,100)
        prob.append(b)
        
        i += 1

    print(prob)

problem: i'm getting the list 'prob' printed in infinite loop

How to Tackle this Problem

coarse boltBOT
#

@craggy steeple

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

craggy steeple
#

Kindly help!

opal spear
#

would you mind fixing the syntax highlighting?

#

thx

craggy steeple
viscid zodiac
craggy steeple
viscid zodiac
#

A while True loop. Or are you asking why the other loops aren't entered?

craggy steeple
#

no, i wanna tackle the problem: that the prob[] is beign printed as an infinite loop, which i did'nt intended to do

viscid zodiac
#

It's doing that because you're printing inside of an infinite loop.

#

You have a while True loop with no breaks, so it will loop forever, and print prob each time it loops.

craggy steeple
#

import random

level = 0
while True:
    while (level != 1) and (level != 2) and (level != 3):
        level = int(input("Level: "))

        if (level == 1) or (level == 2) or (level == 3):
            break

    i=0
    a=0
    b=0
    prob = []
    while i != 10:

        a = random.randint(1,100)
        prob.append(a)

        b = random.randint(1,100)
        prob.append(b)

        i += 1

    sys.exit()

print(prob)

wht if i do this?

viscid zodiac
#

Why sys.exit? That will kill your program.

craggy steeple
#

what to do then instead of it?

floral rivet
viscid zodiac
#

But an unconditational break there would make the loop itself useless. I'm currently trying to figure out what the purpose of the while True loop is

#

I don't think you even want the outer loop. I think it's the problem.

craggy steeple
#

it worked ! tnx guys

craggy steeple
viscid zodiac
#

If you have an unconditional break at the end of a loop, that almost certainly means you don't want the loop in the first place. break there is just covering up that the loop is the issue.

#

Why did you put the while True loop there in the first place?

craggy steeple
#

no no, the break at the end, is being executed only after everything is completed, that's why i think, no other bug occured

viscid zodiac
#

Yes, but again, having a break at the end of a loop here means you shouldn't have that loop at all.

#

You generally shouldn't have useless code, since that just distracts from what's happening.

neon shell
silk nest
#

Also, should we talk about the second while loop?

craggy steeple
viscid zodiac
#

Are you expecting the break in the inner while loop to break from all the loops?

craggy steeple
#

no, i'm expecting it to break from the inner while loop which checks for the level number

#

however i'm expecting from the last break to break from the whole outer while True loop

viscid zodiac
#

Fun fact: apparently the compiler will completely optimize the loop out in a case like this. I didn't think it did such optimizations:

#

!e

from dis import dis

dis("""
while True:
    print(1)
    break
""")
coarse boltBOT
neon shell
neon shell
craggy steeple
viscid zodiac
# craggy steeple What is this?

Sorry, maybe ignore that. That was more for others. That's the code that Python compiles to. The compiler recognized that the while loop was useless and basically removed the loop from the code.

silk nest
#

Yeah, don't worry about the bytecode

neon shell
viscid zodiac
#

If the "loop" still "existed" by the time the code executed, there would be a jump-backward instruction there.

craggy steeple
#

Now guys i want to input 2 values at a time from the list 'prob'

craggy steeple
silk nest
#

It should be EITHER

while level not in (1,2,3):
    level = int(input("Level: "))

or

while True:
    level = int(input("Level: "))
    if level in (1,2,3):
        break

(The first is preferred.)

#

It's not wrong, it's just more code than it needs to be, so it's confusing for human readers, and the more code you have means the more chances for problems later

#

I'm shocked there's no "walrus" reaction emoji on this ~channel~ server

craggy steeple
#

Lets just leave the formatting part

silk nest
#

Back to the original script.

level = 0
while True:
    ...

If you changed that to

while True:
    level = 0
    ...
#

Then it would continually ask for "Level:" and print prob, then ask again for another Level

#

because you're resetting level = 0 at the start of every loop, and 0 is not in (1,2,3) so calls the input() function again

craggy steeple
#
import random

level = 0
while True:
    while level not in (1,2,3):
        level = int(input("Level: "))

        if level in (1,2,3):
            break

    i=0
    a=0
    b=0
    proba = []
    probb = []
    while i != 10:

        a = random.randint(1,100)
        proba.append(a)

        b = random.randint(1,100)
        probb.append(b)

        i += 1

    break

for num in proba:
    num = x
    if num == proba[0]:
        q1 = [x]

    if num == proba[0]:
        q1 = [x]

    if num == proba[0]:
        q1 = [x]

    if num == proba[0]:
        q1 = [x]

    if num == proba[0]:
        q1 = [x]
print(prob)

now for this part

for num in proba:
    num = x
    if num == proba[0]:
        q1 = [x]

    if num == proba[0]:
        q1 = [x]

    if num == proba[0]:
        q1 = [x]

    if num == proba[0]:
        q1 = [x]

    if num == proba[0]:
        q1 = [x]
    ...

Now is there anyother way to do this? instead of just copypasting the code

craggy steeple
silk nest
#

Those all do the same thing. so I'm not sure what the intention is here. presumably you want to loop through proba?

craggy steeple
silk nest
#

So the previous loop was doing

def generate_integer(level):
    ...

And filling 2 lists of ten integers between 10 & 100

#

which is actually not what you're supposed to be doing. it's supposed to be exactly "level" number of digits

craggy steeple
#

i'm not getting it, do you mind helping me through vc?

silk nest
#

No, I think discord chat is good enough

craggy steeple
#

oh okay, sure lets go with chat

#

So can you summarize what you're talking about in a nutshell

silk nest
#

in the original script, you're not using level anywhere, it's supposed to be the lengths of the numbers

craggy steeple
#

no, i'm gonna use level later

#

it's nothing just an instruction by the assignment-makers

silk nest
#
    proba = []
    probb = []
    while i != 10:
        a = random.randint(1,100)
        proba.append(a)
        b = random.randint(1,100)
        probb.append(b)
        i += 1

should be

    proba = []
    probb = []
    while i != 10:
        a = generate_integer(level)
        proba.append(a)
        b = generate_integer(level)
        probb.append(b)
        i += 1

right?
and I'm saying what you have is equivalent to

def generate_integer(level):
    return random.randint(1,100)
craggy steeple
#

Have you read that assignment question i gave you, you'll understand there what i'm trying to achieve and my intention

silk nest
#

n is level

#

But anyways, assuming proba and probb are filled with the right digits... lets look at your next loop

#

You want to loop through pairs of them, right? the first element of each, the second element of each, etc.

craggy steeple
craggy steeple
silk nest
#

Is that not the assignment?

craggy steeple
#

it is

silk nest
#

anyways, there's a few ways to loop through the two lists as pairs. We'll go with the i index mode you're using in the rest of the script

#
i = 0
while i < 10:
   x = a[i]
   y = b[i]
   i += 1

or the nicer option

#
for i in range(10):  # range produces (0, 1, 2, ... 9) up to but not including the number given
   x = a[i]
   y = b[i]
#

Is that a good start?

#
for num in proba:
    num = x
    if num == proba[0]:
        q1 = [x]
    ...

What you had before here doesn't make sense, as you're extracting each num from proba, then overwriting it with x (which doesn't exist, maybe you meant x = num). then seeing if that value equals the first element of proba, I guess to determine if we are "at" the first element, but we can use index variable i for that.

#
for i in range(10):
   x = proba[i]
   y = probb[i]

Sorry, I forgot the list-variable names.

coarse boltBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.