#Need help with one of my problems

95 messages · Page 1 of 1 (latest)

south wave
#

Code:

def is_valid_fraction(input_str):
try:
numerator, denominator = map(int, input_str.split('/'))
return denominator != 0
except ValueError:
return False

def is_valid_input(input_str):
try:
numerator, denominator = map(int, input_str.split('/'))
return '/' in input_str and denominator != 0 and input_str.count('/') == 1
except ValueError:
return False

fraction = input("Fraction: ")

if is_valid_input(fraction):
numerator, denominator = map(int, fraction.split('/'))

if numerator == 0:
    print("E")
elif numerator == denominator:
    print("F")
elif numerator == 1 and denominator == 4:
    print("25%")
elif numerator == 1 and denominator == 3:
    print("33%")
elif numerator == 1 and denominator == 2:
    print("50%")
elif numerator == 2 and denominator == 3:
    print("67%")
elif numerator == 3 and denominator == 4:
    print("75%")
elif numerator == 1 and denominator == 100:
    print("E")
elif numerator == 100 and denominator == 100:
    print("F")
elif numerator == 99 and denominator == 100:
    print("F")
else:

else:

wooden shore
#

Can you explain what you need to do?

#

Also, please send code using code blocks

#

!format

cedar bobcatBOT
#
Code Formatting

When sharing code with the community, please use the correct formatting for ease of readability.

Example

```py
YOUR CODE HERE
```

Those are back ticks not single quotes, typically the key above TAB

south wave
wooden shore
#

Do you know what all the possible valid inputs are?

#

Like can they do 1/7 or 999/1000?

south wave
#

Up to a 6th i think

#

above that no

#

but the other requirements are the errors in got

wooden shore
#

That doesn't really answer what I'm trying to ask because one could say 2/200 is less than a sixth

south wave
wooden shore
#

Can you just send a screenshot of what they asked you to do?

south wave
#

alr

#

im sorry i couldnt explain it that well by the way, but here.

wooden shore
#

Okay so my intuition was correct

#

You can do things like 1/7, 2/100, 99/2313412 etc

#

That means there are an infinite number of possible inputs

#

And therefore using a whole bunch of if elif else won't work

#

hopefully that makes sense?

south wave
#

yeah but i just dont understand the errors. like expected program to reject but did not

wooden shore
#

Yeah I'm not sure either, but I think the best thing to do right now is to restart over again because it seems like you didn't know how to approach the problem in the right way so you ended up with really messy code

south wave
#

okay

#

thanks

wooden shore
#

Let's start easy which is just getting the user input

#

something like fraction = input("Fraction: ") right?

#

What do we do from there

south wave
#

define numerators and denominators?

#

or trying to do something like: def is_valid_fraction(input_str): try: numerator, denominator = map(float, input_str.split('/')) return denominator != 0 except ValueError: return False

then

    try:
        numerator, denominator = map(float, input_str.split('/'))
        return '/' in input_str and denominator != 0 and input_str.count('/') == 1
    except ValueError:
        return False```
wooden shore
#

My guy, there's a reason why I'm telling you to restart lol

south wave
#

thats what id do if i restarted

#

bc tbh i have no clue what else to do after that than the last code i did

wooden shore
#

Let's go step by step and not make it too complicated at first

#

We start of with this ```py
fraction = input("Fraction: ")

south wave
#

okay

wooden shore
#

Let's now make a function to validate the input

#
fraction = input("Fraction: ")
def is_valid_input(fraction):
  ...
#

What do you think the first thing we should check is

south wave
#

im unsure

wooden shore
#

Well, I'd think that we should check if / exists in the input right?

#

So something like

#
fraction = input("Fraction: ")

def is_valid_input(fraction):
  if "/" not in fraction:
    return False
  ...
#

Doesn't that make sense?

south wave
#

yeah

wooden shore
#

So now we know we have a slash

#

What's next?

south wave
#

making numerator and denominator valid interger values?

wooden shore
#

Aren't you skipping a few steps?

#

What about splitting the input by the slash?

#

You've got to get into the habit of breaking down problems into the smallest pieces and figure out the plan step by step

south wave
#

i only started on the 1st, i dont know too much

#

how would i do that

wooden shore
#

That's why I'm helping you walk through the thinking process

#

Because you've obviously gone too far ahead and developed some bad habits

wooden shore
#

Now we know that fraction is a string that has at least one / in it.

#

How do we split it up by /?

south wave
#

sep?

wooden shore
#

Give me the line that will do it

south wave
#

i dont know

wooden shore
#

How do you split the string variable fraction by the slashes?

south wave
#

sep="/"

#

?

wooden shore
#

Okay

#

And then split fraction

south wave
#

sep="/" + fraction

wooden shore
#

Does that work?

wooden shore
#

Did you write that yourself?

south wave
#

it was me and my friend

#

but he isnt here

wooden shore
#

Hmmm

south wave
#

mostly my friend nevertheless

wooden shore
#

Makes sense

south wave
#

he tried explaining it and it never made sense

wooden shore
#

Aren't you taking CS50P?

south wave
#

i only did the if elif else

south wave
wooden shore
#

What lecture are you on now?

south wave
#

4th one? its the one that explains, SynthaxError, ValueError, try and except, and else

wooden shore
#

May I suggest you revisit the lectures from the beginning and actively follow along with them

#

Because if you did follow the lectures properly, the question I'm asking you should be comfortably answerable from lecture 0

south wave
#

i took notes its just i dont understand what most of the questions mean

#

also i dont think sep was introduced in lecture 0

wooden shore
#

Taking notes is different to understanding the content

wooden shore
south wave
#

i take the notes so i can understand afterwards bc hearing it never makes me learn

#

doing it does

wooden shore
#

I agree, you should be coding alongside David and testing how it works and what breaks

south wave
#

okay

wooden shore
#

I'm not saying all this to imply you're a bad student or you're not good at learning. I'm just trying to help you and suggest what is best for you at this moment

#

Because right now, you don't have the fundamental understanding and conceptual knowledge to finish this homework problem by yourself

#

Also, I would suggest you to not rely on your friend to code things up for you. They shouldn't be doing the thinking for you because studies show that that's a terrible way to learn

#

Feel free to come back and ask questions if you ever get stuck or need help though!