This is my python code: Only output that i get in the end is "True"
Why am i not receiving output for others? Please help.
I am using jupyter notebook on visual studio code. Kernel: Python 3.9 64-bit
# RELATIONAL OPERATORS
num1 = 10
num2 = 0
num3 = 10
str1 = "good"
str2 = "life"
# Equals to
num1 == num2
str1 == str2
# Not equal to
num1 != num2
str1 != str2
num1 != num3
# Greater Than
num1 > num2
str1 > str2
# Less Than
num1 < num3
str2 < str1
# Greater than or equal to
num1 >= num2
num2 >= num3
str1 >= str2
#Less then or equal to
num1 <= num2
num2 <= num3
str1 <= str2
You'll have to wrap each of the statements in print()
num1 = 10
num2 = 0
num3 = 10
str1 = "good"
str2 = "life"
print(num1 == num2)
print(str1 == str2)
# so on and so forth
Related
Write a recursive function called print_num_pattern() to output the following number pattern.
Given a positive integer as input (Ex: 12), subtract another positive integer (Ex: 3) continually until 0 or a negative value is reached, and then continually add the second integer until the first integer is again reached.
For coding simplicity, output a space after every integer, including the last. Do not end output with a newline.
Ex. If the input is:
12
3
the output is:
12 9 6 3 0 3 6 9 12
My code:
def print_num_pattern(num1,num2):
if (num1 == 0 or num1 < 0):
print(num1, end = ' ')
return
print(num1, end = ' ')
if num1 - num2 <= 0:
return
print_num_pattern(num1 - num2, num2)
print(num1, end = ' ')
if __name__ == "__main__":
num1 = int(input())
num2 = int(input())
print_num_pattern(num1, num2)
My output
12 9 6 3 6 9 12
Expected output
12 9 6 3 0 3 6 9 12
def print_num_pattern(num1,num2):
if (num1 == 0 or num1 < 0):
print(num1, end = ' ')
return
print(num1, end = ' ')
print_num_pattern(num1 - num2, num2)
print(num1, end = ' ')
if __name__ == "__main__":
num1 = int(input())
num2 = int(input())
print_num_pattern(num1, num2)
while adding two numbers, I want to check how many times carry is occurring –
for eg:
Input
Num 1: 451
Num 2: 349
Output
2
Explanation:
Adding ‘num 1’ and ‘num 2’ right-to-left results in 2 carries since ( 1+9) is 10. 1 is carried and (5+4=1) is 10, again 1 is carried. Hence 2 is returned.
def NumberOfCarries(num1, num2):
count = 0
l = str(num1)
i = 0
if i <= len(l):
n = num1 % 10
n2 = num2 % 10
sum = n + n2
print(f" num is {num1} and {num2} n1 is {n} and n2 is {n2} and sum is {sum}")
if sum > 9:
count += 1
num1 = num1 // 10
num2 = num2 // 10
i += 1
else:
num1 = num1 // 10
num2 = num2 // 10
i += 1
return count
num1 = int(input("> "))
num2 = int(input("> "))
print(NumberOfCarries(num1, num2))
Here loop is not working, only one time the sum is generating. I want to generate for each number in numb1. I tired with while, if and for. Please help me.I am new to this
I think you tried to do this:
def number_of_carries(num1, num2):
amplitude_num1 = len(str(num1))
amplitude_num2 = len(str(num2))
count = 0
i = 0
carry_over = 0
while i < amplitude_num1 or i < amplitude_num2:
n = num1 % 10
n2 = num2 % 10
sum_of_digits = n + n2 + carry_over
print(f" num is {num1} and {num2}, n1 is {n} and n2 is {n2}, carry digit from previous addition is {carry_over}, sum is {sum_of_digits}")
if sum_of_digits > 9:
count += 1
carry_over = 1
else:
carry_over = 0
num1 //= 10
num2 //= 10
i += 1
return count
num1 = int(input("> "))
num2 = int(input("> "))
print(number_of_carries(num1, num2))
But if you want to have a solution that would accept more that 2 numbers this could be modified to:
def number_of_carries(numbers):
amplitude = max(len(str(x)) for x in numbers)
count = 0
i = 0
carry_over = 0
for i in range(amplitude):
current_numbers = tuple(x % 10 for x in numbers)
sum_of_digits = sum(current_numbers) + carry_over
print(
f" numbers are {' '.join(str(x) for x in numbers)}, "
f"current_numbers are {' '.join(str(x) for x in current_numbers)}, "
f"carry digit from previous addition is {carry_over}, sum is {sum_of_digits}"
)
if sum_of_digits > 9:
count += 1
carry_over = sum_of_digits // 10
numbers = tuple(x // 10 for x in numbers)
return count
input_numbers = (198, 2583, 35)
print(number_of_carries(input_numbers))
today I wrote a simple math game that I can practice mental math. My concerns is to make sure two number are always divisible. I have tried the while loop to add 1 until it divisible but it took too long:
import random
import operator
def random_problem():
operators = {
'+': operator.add,
'-': operator.sub,
'x': operator.mul,
':': operator.truediv
};
num1 = random.randint(1000,9999)
num2 = random.randint(num1,9999)
operation = random.choice(list(operators.keys()))
if operation == ':':
while num1 % num2 != 0:
num2 += 1
answer = (operators.get(operation)(num1, num2))
print(f'What is {num1} {operation} {num2}')
return answer
So any ideas to make this process faster? Thanks for your answers.
A simple approach would be:
Generate a random first number (n1)
Generate a random multiplier (m)
Use the product of the first number and the multiplier as the second number (n2 = n1 * m)
IIUC:
Your current code will only work when num1 == num2 for all other values of num2 the denominator is always greater than the numerator so the modulus will never be 0, so you would get an infinite loop.
Instead try generating the numerator from the denominator like:
Code:
import random
import operator
def random_problem():
operators = {
# '+': operator.add,
# '-': operator.sub,
# 'x': operator.mul,
':': operator.truediv
}
num2 = random.randint(1000, 2000)
num1 = num2 * random.randint(1, int(9999/num2))
operation = random.choice(list(operators.keys()))
if operation == ':':
while num1 % num2 != 0:
num2 += 1
answer = (operators.get(operation)(num1, num2))
print(f'What is {num1} {operation} {num2}')
return answer
print(random_problem())
Output:
What is 4136 : 1034
4.0
I need to compare 2 numbers,
if they have the same sign (positive or negative), print "same sign".
If they have a different sign, print "different sign"
The catch is, I need to do it without the use of < or > (greater than or less than) and only using addition and subtraction of num1 and num2. You can also use 0 (no other numbers).
Here is what it looks like with the <>s:
num1 = int(input("enter num1: "))
num2 = int(input("enter num2: "))
if num1 < 0 and num2 < 0: print("same sign")
if num1 > 0 and num2 > 0: print("same sign")
if num1 > 0 and num2 < 0: print("different sign")
if num1 < 0 and num2 > 0: print("different sign")
You can check whether two numbers x and y have the same sign by validating the following:
same_sign = abs(x) + abs(y) == abs(x + y)
Well, mb not the prettiest solution, but have a check
#!/usr/bin/env python3
num1 = 10
num2 = 2
if ((num1 & 0x800000) == (num2 & 0x800000)):
print('same sign')
else:
print('different sign')
the trick here, that int type in Python takes 24 bits = 3 bytes. Signed types have 1 in the most significant position. 0x800000 = 1000 0000 0000 0000 0000 0000b. If both nums have this bit - same sign, otherwise - different.
A little late here, but this is what I figured out.
def sameSign(x, y)
if (x*y > 0):
print("same sign")
else:
print("different sign")
Negative times negative and positive time positive both always give positive. Negative time positive gives negative. If you pass in zero, you always get false, so you could add a check.
You can use subtract the number by itself and if the result equal to zero in the two numbers or non equal to zero is the two numbers then it is the same sign, else different sign, here is the code:
num1 = int(input("enter num1: "))
num2 = int(input("enter num2: "))
if num1 + 0 - num1 == 0 and num2 + 0 - num2 == 0: print("same sign") # +
elif num1 + 0 - num1 != 0 and num2 + 0 - num2 != 0: print("same sign") # -
else: print("different sign")
Note: I am using Python 3
I am trying to figure out how to print all other numbers that are set in the variables (in this case num1, num2, num3 and num4) except for zeros. I have been playing around with my code for the last 2 to 3 days and have not found any solution yet. I searched on the internet for any tutorials / code examples but still to no avail.
num1 = 0
num2 = 2
num3 = 3
num4 = 4
if num1 != 0:
test1 = num1
elif num2 != 0:
test2 = num2
elif num3 != 0:
test3 = num3
elif num4 != 0:
test4 = num4
print(test1, test2, test3, test4)
Here is the error that I keep on getting when I run the code above:
NameError: name 'test1' is not defined
I am sure that this is a pretty simple problem and that I am just missing something.
Thanks in advance.
One option is to place all of the variables in a list is so...
numbers = [num1, num2, num3, num4]
Then use a loop to print to ones not equal to zero:
for num in numbers:
if (num != 0):
print(num)
Here, the statement test1 = num1 would only get executed if the statement if num1 != 0: is true, which in your case is not. So, the test1 variable is not even getting created.
So, if you can create empty containers for test1, test2, test3 and test4; that would solve the purpose.
The code would look something like this:
num1 = 0
num2 = 2
num3 = 3
num4 = 4
test1 = test2 = test3 = test4 = 0
if num1 != 0:
test1 = num1
elif num2 != 0:
test2 = num2
elif num3 != 0:
test3 = num3
elif num4 != 0:
test4 = num4
print(test1, test2, test3, test4)
Output: (0, 2, 0, 0)
That's because the variables test...n have no assigned values at the time of usage
Try this:
num1 = 0
num2 = 2
num3 = 3
num4 = 4
test1=test2=test3= test4= int()
if num1 != 0:
test1 = num1
elif num2 != 0:
test2 = num2
elif num3 != 0:
test3 = num3
elif num4 != 0:
test4 = num4
print(test1, test2, test3, test4)
Prints out : (0,2,0,0)