milon

joined 1 year ago
[–] [email protected] 3 points 1 week ago

Thank you for the thorough explanation. It makes sense to me why I had the error that I did. I'll keep this in mind next time when I consider using a bitwise operator.

[–] [email protected] 6 points 1 week ago (7 children)

Thank you for the reply. It seems bitwise operators are somewhat of an advanced concept that I may revisit down the road.

[–] [email protected] 4 points 1 week ago (12 children)

Thanks. I think I understand why I wouldn't want to use it in this case. But what is an example of where I can use it? This makes me think I should avoid using bitwise operators with integers and keep it to strings only, but I know that's not true from what I've learned.

[–] [email protected] 2 points 1 week ago

Thank you. Not sure why in the link the arithmetic in green results in 7.

[–] [email protected] 2 points 1 week ago* (last edited 1 week ago)

Thank you for breaking it down.

I'm just now sure when it is appropriate to use '|'. If bitwise operators can only be used with integers (and not floats), what's an example where I can use it.

[–] [email protected] 1 points 1 week ago

Yes I did eventually think of that as well but just wanted to understand why '|' wasn't producing the results I expected.

[–] [email protected] 1 points 1 week ago (2 children)

I did come across that link but didn't quite understand it. If looking only at 25 | 10, does the code not run as expected because 25 is 5 digits long and 10 is 4 digits long? Is that what's meant by "two equivalent length bit designs"?

Also, I can't tell if 10 | 4 = 7 or 10 | 4 = 14.

 

if coin == 25 | 10 | 5:

If I replace the '|' with 'or' the code runs just fine. I'm not sure why I can't use '|' in the same statement.

Doing the following doesn't work either:

if coin == 25 | coin == 10 | coin == 5:

I know bitwise operators can only be used with integers, but other then that is there another difference from logical operators?

 
 # Ask user to enter an expression and display output
def main():
    expression = input("Expression: ")

    print(calculate(splitter(expression)))


# Split expression into components and assign to variables as float values
def splitter(expression):
    x, y, z = expression.split()

    return x, y, z

# Calculate expression result
def calculate(x, y, z):
    x, z = float(x), float(z)

    if y == "+":
        return str(round((x + z), 1))
    elif y == "-":
        return str(round((x - z), 1))
    elif y == "*":
        return str(round((x * z), 1))
    else:
        return str(round((x / z), 1))



main()

I am getting traceback errors for any expression (1 + 1) I enter.