#πŸ”’ I need help to why my code is breaking when the if statement is suppose to be FAlse

27 messages Β· Page 1 of 1 (latest)

glass tundra
#
    data = [[], [], []]
    # solution = []
    for problem in problems:
        elements = problem.split(' ')
        print(elements)
        if len(elements[0]) | len(elements[2]) > 4:
            print('Error: Numbers cannot be more than four digits.')
            break
        elif len(elements[2]) >= len(elements[0]):
            second = f'{elements[1]} {elements[2]}'
        else:
            second = f'{elements[1]}{" " * (len(elements[0]) - 1)}{elements[2]}'
            print(second)

arithmetic_arranger(["3 / 855", "3801 - 2", "45 + 43", "123 + 49"])```

output: 

```['3', '/', '855']
['3801', '-', '2']
Error: Numbers cannot be more than four digits.

the output is giving me the error print in the pfirst part of the if statement. Even though the len of the third part of the parameter isnt > 4 its still outputing the error statement and athan the break

What I want to do is to go through all the parameter and output it as like the first two

.........like this >>>>>>>>>>>>>>> ['3', '/', '855'] ['3801', '-', '2']

pastel ravenBOT
#

@glass tundra

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.

#

Hey @glass tundra!

It looks like you pasted Python code without syntax highlighting.

Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
last pollen
#

!e

elements = ['3801', '-', '2']
print(len(elements[0]) | len(elements[2]))
pastel ravenBOT
last pollen
#

What are you expecting to happen here?

#

| is a bitwise OR operation, so 4 | 1 is basically 0100 | 0001, which joins to become 0101 and that == 5. πŸ™‚

#

@glass tundra ?

glacial bolt
last pollen
#

They split them into a list of strings, so that's just length of a string.

last pollen
#

Well, whenever they return here, I think maybe you intend to restrict the length of the numbers in the operation.
First and foremost | is not the same as the or keyword. The former is bitwise OR, as shown above; while the keyword or is logical (true/false).

Further:

if len(elements[0]) | len(elements[2]) > 4:

This may fall into the category of the or gotcha.

#

!or-gotcha

pastel ravenBOT
#
The or-gotcha

When checking if something is equal to one thing or another, you might think that this is possible:

# Incorrect...
if favorite_fruit == 'grapefruit' or 'lemon':
    print("That's a weird favorite fruit to have.")

While this makes sense in English, it may not behave the way you would expect. In Python, you should have complete instructions on both sides of the logical operator.

So, if you want to check if something is equal to one thing or another, there are two common ways:

# Like this...
if favorite_fruit == 'grapefruit' or favorite_fruit == 'lemon':
    print("That's a weird favorite fruit to have.")

# ...or like this.
if favorite_fruit in ('grapefruit', 'lemon'):
    print("That's a weird favorite fruit to have.")
last pollen
#

You'll actually need to spell things out:

if len(elements[0]) > 4 or len(elements[2]) > 4:

You might also make life easier by unpacking them into separate variables first:

left, op, right = problem.split(' ')
if len(left) > 4 or len(right) > 4:
glass tundra
#

o so i have to use the > 4 on both side

last pollen
#

Correct. There's no simple way to write that once and apply it to many things, as you might in an english sentence.

glass tundra
#

ok it works now

#

thanks for the help

last pollen
#

There is something like:

if max([len(elements[0]), len(elements[2])]) > 4:

But that starts getting too verbose.

glass tundra
#

also

#

i cant use the | instead of β€œor”

last pollen
glass tundra
#

thanks

pastel ravenBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, 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.