#๐Ÿ”’ Code review

93 messages ยท Page 1 of 1 (latest)

unkempt pier
#

I have one check that I cannot seem to pass, my code generally works fine.

import random

def main():
    lvl = get_level()
    x, y = generate_integer(lvl), generate_integer(lvl)
    score = 0
    count = 0

    wrong = 0

    r = 10

    for z in range(r*3):

        awns = input(f"{x} + {y} = ")

        if awns.isnumeric() and int(awns) == x + y:
            score += 1
            if score == 10 or (score + wrong) == 10:
                break
            else:
                x, y = generate_integer(lvl), generate_integer(lvl)
                continue
        else:
            if count == 2:
                count = 0
                print(x + y)
                x, y = generate_integer(lvl), generate_integer(lvl)
                wrong += 1
                continue
            else:
                print("EEE")
                count += 1
                continue

    print("score:", score)

def get_level():
    while True:
        n = input("level: ")
        if n.isnumeric() and int(n) in range(1, 4):
            return int(n)
        else:
            continue

def generate_integer(level):
    if level not in (1, 2, 3):
        raise ValueError()

    min_int = 10**(level-1) - 1
    max_int = 10**level - 1

    if level == 2:
        min_int = 10
    elif level == 3:
        min_int = 100

    return random.randint(min_int, max_int)

if __name__ == "__main__":
    main()
celest crowBOT
#

@unkempt pier

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.

unkempt pier
#

i think something is wrong with the else statment in main

unkempt pier
#

The program is fine when i do it myself

#

Just the checks arnt checking

vapid blaze
#

Can you give the description of the problem you was trying to solve?

unkempt pier
#

sure

#

link too

#

what u think saad

vapid blaze
#

I am thinking it gas to do with printing the right answer after 3 fails

unkempt pier
#

it works for other tests but not a complicated one

proven helm
#

Is the problem because it thinks when you generate 10 different sum challenges using 1 digit numbers, one challenge has to contain an 8?

Is there a requirement to use as many different summands as possible? Otherwise I don't understand the test - it seems a bit flakey given it asks you for random outputs.

unkempt pier
#

the check gets 2 wrong on purpose

proven helm
#

8 for 6 + 6 ?

unkempt pier
#

no it shouldnt display 6+6 at all

vapid blaze
#

Run program again

proven helm
#

Whats with this?:

    if level not in (1, 2, 3):
        raise ValueError()
vapid blaze
#

We need to see another fail text

vapid blaze
proven helm
#

Oh yeah I getchya, it's a useful error. Thanks

vapid blaze
proven helm
#

You know str.isnumeric supports the edge case of U+2155, VULGAR FRACTION ONE FIFTH ?

Python documentation

The following sections describe the standard types that are built into the interpreter. The principal built-in types are numerics, sequences, mappings, classes, instances and exceptions. Some colle...

feral widget
# unkempt pier

This is saying that out of all of the 10 problems, it expects that it answered 8 of them correctly in the output

proven helm
#

Thanks - I get it now

feral widget
#

Took me awhile to understand the test output results

proven helm
#

How is count reset to 0 after a correct answer?

feral widget
#

Doesn't look like OP did reset the count to 0

proven helm
#

main is a bit messy to be honest. Refactor out the three different parts into smaller functions. E.g. getting a new puzzle, solving each puzzle (including the three goes), and scoring.

feral widget
unkempt pier
#

hi back

proven helm
#

You're not resetting count after a correct answer

unkempt pier
#

ok ill do that now

proven helm
#

It was hard to tell because that whole code block is so complicated.

feral widget
unkempt pier
#

added this

#

still same error

proven helm
#

Fire her up and let us know how you get on

unkempt pier
feral widget
#

Dont spoonfeed please

unkempt pier
#

yea saad thanks but its not good practice

feral widget
#

The whole point of doing CS50 exercise is to exercise your programming and critical thinking skills

#

Can you delete this please?

#

Thanks

vapid blaze
#

Oh sorry

feral widget
proven helm
#

Those continue statements are superfluous given the else: and there's nothing else in the loop afterwards

unkempt pier
# feral widget Can you share your full latest code for now?
import random

def main():
    lvl = get_level()
    x, y = generate_integer(lvl), generate_integer(lvl)
    score = 0
    count = 0
    wrong = 0
    r = 10

    for z in range(r*3):

        awns = input(f"{x} + {y} = ")

        if awns.isnumeric() and int(awns) == x + y:
            count = 0
            score += 1
            if (score + wrong) >= 10:
                break
            else:
                x, y = generate_integer(lvl), generate_integer(lvl)
                continue
        else:
            if count == 2:
                count = 0
                wrong += 1
                print(x + y)
                x, y = generate_integer(lvl), generate_integer(lvl)
                continue
            else:
                count += 1
                print("EEE")
                continue

    print("score:", score)
#

here

proven helm
#

What's it actually saying the score is, when it's supposed to be 8?

feral widget
unkempt pier
#

its at the bottom

proven helm
#

Just make generate_integer return 0 and run some simple tests

vapid blaze
#

Check for score + wrong >= 10 at beginning of loop

proven helm
unkempt pier
unkempt pier
vapid blaze
#

It keeps on looping forever

unkempt pier
#

yea its inside a loop

proven helm
vapid blaze
unkempt pier
vapid blaze
#

Also I prefer a while True loop

unkempt pier
#

works now

feral widget
proven helm
#

Well done!

unkempt pier
#

but yea while True is valid too

proven helm
feral widget
#

Here's how I did it:

    score = 0
    questions = 10
    while questions > 0:
        retries = 3
        x, y = generate_integer(lvl), generate_integer(lvl)
        while retries > 0:
            answer = input(f"{x} + {y} = ")
            try:
                if x + y == int(answer):
                    score += 1
                    questions -= 1
                    break
                else:
                    raise ValueError()
            except ValueError:
                print("EEE")
                retries -= 1
            if retries == 0:
                questions -= 1
        print(x + y)
    print(f"Score: {score}")
proven helm
#

For problems like this where the number of iterations is crystal clear in the requirements, a for __ in range(N): loop is obviously superior to while True:

feral widget
unkempt pier
feral widget
unkempt pier
#

ok thanks ill keep it in mind

#

!close

celest crowBOT
#
Python help channel closed with !close

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.