Show-Me-the-Code / python

Show Me the Code Python version.
https://github.com/Show-Me-the-Code/show-me-the-code
MIT License
3.93k stars 2.74k forks source link

Conditionals #269

Open coded92 opened 6 years ago

coded92 commented 6 years ago

Please can you help solve this in Python and explain to me

mystery_int_1 = 7 mystery_int_2 = 2

You may modify the lines of code above, but don't move them! When you Submit your code, we'll change these lines to assign different values to the variables.

The variables below hold two integers, mystery_int_1 and mystery_int_2. Complete this program below such that it prints "Factors!" if either of the numbers is a factor of the other. If neither number is a factor of the other, do not print anything.

Hint: You can do this with just one conditional statement by using the logical expressions (and, or, and not). You'll also use the modulus operator

Please help me with the Add your code here!

Have tried several means am just not getting it

lionpower7 commented 6 years ago

Answer: For Python.

mystery_int_1 = int() mystery_int_2 = int()

mystery_int_1 = input("Enter a Number:") mystery_int_2 = input("Enter another Number:")

if mystery_int_1 % mystery_int_2 == 0 or mystery_int_2 % mystery_int_1 ==0: print("Factors!") else: print(" ")

UHungLin commented 6 years ago

Answer by Python.

mystery_int_1 = int( input("Enter a Number: "))
mystery_int_2 = int(input("Enter another Number: "))

if mystery_int_1 or mystery_int_2 == 0:
    print("Factors!")
elif mystery_int_1 % mystery_int_2 == 0 or mystery_int_2 % mystery_int_1 ==0:
    print("Factors!")
else:
    print(" ")
19ANDY commented 6 years ago

In this the variables are assigned the value which you have given and hence the code:

mystery_int_1, mystery_int_2 = 7, 2 print("Factors!" if mystery_int_1%mystery_int_2 ==0 or mystery_int_2%mystery_int_1==0 else " ")

But if the user wants to enter the number then the code would be :

mystery_int_1, mystery_int_2 = int( input("")), int(input("")) print("Factors!" if mystery_int_1%mystery_int_2 ==0 or mystery_int_2%mystery_int_1==0 else " ")