Help with simple exercise, usually do c++ not python


# Multiply of 2 integers using if then statements
num1 = int(input("Please enter 1st number: "))
num2 = int(input("Please enter 2nd number: "))

result = num1 * num2

if num1 or num2 > 1000:
    print("Error: Number(s) is greater than 1000, restart program and try again.")
else:
    print("The result is: ", result)

I have tried switching the if then statements around but cannot get the result to display correctly. The first statement works but the 2nd one just displays the first if print.

Its suppose to if a number entered is greater 1000 then its errors out, if they are less than 1000 it multplies them to display the result. If i switch them around and change the first statement then it multplies them and displays the result, but does not error out if a number is greater than 1000.

I am learning python normally i use c++.

An expression with multiple operations, like num1 or num2 > 1000, without parentheses, determines the order in which those operations are applied according to some convention. In this case one that depends on the programming language.

See here for the operator precedence in Python

See how > appears before or in the table. So, it gets applied first and the result of it then gets or with num1.

The result of num2 > 1000 could be True or False, but if num1 is non-zero, then the value of num1 or num2 > 1000, which is that of num1 or (num2 > 1000) will be True.

In C++, the condition would be num1 || num2 > 1000, which is treated as num1 || (num2 > 1000), and that’s the same as what Python does.

You can even write num1 or num2 > 1000 in C++ and it’s the same. This prints true:

#include <iostream>

int main() {
  int num1 = 42, num2 = 13;
  if (num1 or num2 > 1000)
    std::cout << "true";
}

Attempt This Online!

Will be num1.

Ah ok, I see i got it working now thank you guys.