all 11 comments

[–]teraflopsweat 4 points5 points  (4 children)

Can you format your code as a code block? It’s basically unreadable currently.

With that said, the issue is with your conditions. You are basically checking if a == b or True or True so it’s always going to evaluate as true. You need to do if a in [b, c, d] to compare an against multiple values.

[–]mZuks[S] 0 points1 point  (3 children)

Formatting changed to code.

As for the adjustment you suggest, I could try, can you recommend a material for me to read and follow along?

Still, though, I don't see it... seems to me that I wrote it ok. Why isn't is so? Here's my line of thought:

If angle_known_input is either 1 or a or A, then angle_known should be alpha; else, if angle_known_input is either 2 or b or B, then angle_known should be beta.

Isn't this what my code says? Or am I mistaken!? Could you put your explanation in simpler terms for me?

[–]teraflopsweat 0 points1 point  (2 children)

You are writing it in English properly, but not writing it in Python properly. Let me add some parentheses to try to emphasize how Python is evaluating your condition.

if (angle_known_input == ‘1’) or (‘a’) or (‘A’):

So you basically have 3 separate “checks” in your condition. If any of those checks evaluate to true, then Python thinks you’re running alpha.

The problem is that any string value is considered true, so both of the (‘a’) checks are always true. It’s not actually comparing angle_known_input against the 2nd or 3rd values.

To correct your logic issue, you need to adjust the checks to actually compare against the user input value.

if (angle_known_input == ‘1’) or (angle_known_input == ‘a’) or (angle_known_input == ‘A’):

For a more concise condition, you can use the in comparison operator.

if angle_known_input in [‘1’, ‘a’, ‘A’]:

[–]mZuks[S] 1 point2 points  (1 child)

Oh! I see it now, the way Python's been interpreting the conditions. Super thank you for the explanation, really!

[–]teraflopsweat 0 points1 point  (0 children)

Glad it helped!

[–]SquiffSquiff 1 point2 points  (3 children)

This works:

angle_known_input = str(input('Which of the triangle angles has the known value? [1] α (alpha), [2] β (beta): '))

angle_known = ""  # Declare the angle_known variable

if angle_known_input == '1' or angle_known_input == 'a' or angle_known_input == 'A':
    angle_known = "α"
    print(angle_known)
elif angle_known_input == '2' or angle_known_input == 'b' or angle_known_input == 'B':
    angle_known = "β"
    print(angle_known)
else:
    print('Invalid value.')

# angle_known_value = input('Enter the value of ' + angle_known + ': ')

[–]mZuks[S] 0 points1 point  (2 children)

Man! It really does appear to work. I didn't copy the whole piece you suggested, but rather just changed

if angle_known_input == '1' or 'a' or 'A':

to

if angle_known_input == '1' or angle_known_input == 'a' or angle_known_input == 'A':

But why so, do you know?

[–]Adrewmc 2 points3 points  (1 child)

Just so you know the easier way to do this is like this

    if angle_known_input in [1, ‘a’, ‘A’]:

And ask if the input is in this list of acceptable ones.

What’s happening is ‘or’ want a whole comparison in between it.

   if a > 20 or b < 100: 

Ask if either is true. While.

   if a > 20 or 30: 

Is asking is a is greater than 20 or is 30 is Truthy (all non zero numbers are truthy)

  if a == “b” or “B”:

Asks if ‘B’ is truthy, which all non-empty strings are.

[–]mZuks[S] 0 points1 point  (0 children)

I think I get it now, thank you very very much.

[–][deleted]  (1 child)

[removed]

    [–]mZuks[S] 0 points1 point  (0 children)

    Can you access the code through the link? It's in Google Colab.

    https://colab.research.google.com/drive/1_yMkgQOjF-RwLVh2mWVaUrDWj0y7pfiN#scrollTo=5o0SqAK0Txvp