I need to make two different types of codes to find GCD.
I'm not allowed to use def() because I didn't learn it yet.
This is my first code. I am not supposed to use the euclid method here.
I need to input two numbers together,
and set the smaller one to 'bound'.
num1, num2 = (input("input two integers to find GCD: ")).split()
num1=int(num1); num2=int(num2)
if num1 > num2:
bound == num2
elif num1 < num2:
bound == num1
i=2
while (i <= bound) :
if (num1 % i == 0) and (num2 % i == 0):
i == GCD
i=i+1
print("The GCD for %d, %d is %d."%(num1,num2,GCD))
This is my second code. Here, I need to be using the euclid method.
I have tried my best, but I can't catch my errors.
num1, num2 = (input("input two integers to find GCD: ")).split()
num1=int(num1)
num2=int(num2)
temp=num1%num2
while (num1 != 0):
num1=num2;
num2=tmp;
tmp=num1%num2
print("The GCD for %d, %d is %d."%(num1, tmp, num2))
GCD for your first code:
Edit: You were using == instead of =
num1, num2 = (input("input two integers to find GCD: ")).split()
num1=int(num1)
num2=int(num2)
if num1 > num2:
bound = num2
elif num1 < num2:
bound = num1
i=2
for i in range(1, bound+1):
if (num1 % i == 0) and (num2 % i == 0):
gcd=i
i=i+1
print("The GCD for %d, %d is %d."%(num1,num2,gcd))
GCD for your second code without using def using euclid method:
num1, num2 = (input("input two integers to find GCD: ")).split()
num1 = int(num1)
num2 = int(num2)
while num1 != num2:
if num1 > num2:
num1 = num1 - num2
else:
num2 = num2 - num1
print(num1)
Related
I'm new to python and trying to understand recursion. I'm trying to write a code where someone inputs 2 numbers (Num1, Num2). A calculation will take place until Num1 is greater than Num 2. The result of the calculation's final value should then be outputted.
This is my code:
def Recursive(Num1, Num2):
if Num1 > Num2:
return(10)
else:
if Num1 == Num2:
return(Num1)
else:
return(Num1+Recursive(Num1 * 2, Num2))
Num1=input("Enter number 1: ")
Num2=input("Enter number 2: ")
print("Final value: ", Recursive(Num1, Num2))
This is the output that comes out:
Enter number 1: 1
Enter number 2: 15
That's it. There's no output of my print statement. I'm confused as to what I'm doing wrong here and what I should do.
def Recursive(Num1, Num2):
if Num1 > Num2:
return 10
if Num1 == Num2:
return Num1
print(Num1, Num2)
return Num1 + Recursive(Num1 * 2, Num2)
Num1=int(input("Enter number 1: "))
Num2=int(input("Enter number 2: "))
print("Final value: ", Recursive(Num1, Num2))
This should be working code, the reason why your code was not working was because without the int in Num1 = int( input("") ) the program was reading the number as a string. As a result instead of getting 2 from Num1 * 2 when Num1 is 1 you got 11 as it was multiplying a string instead of an integer.
# Here is an example
a = input("Input a number: ")
b = int(input("input a number: "))
print(f"a is a:{type(a)}\n b is a:{type(b)}")
print(f"{a * 2}\n{b * 2}")
Copy the code above input the two numbers and it should give you a better understanding of what I just said.
Write a program whose inputs are three integers, and whose output is the smallest of the three values.
If the input is:
7
15
3
The output is: 3
This is the code I have come up with:
num1 = input()
num2 = input()
num3 = input()
if (num1 < num2):
if (num1 < num3):
smallest_num = num1
elif (num2 < num1):
if (num2 < num3):
smallest_num = num2
else:
smallest_num = num3
print(smallest_num)
This code works for that input, however if you input "29, 6, 17" it returned no output with an error
NameError: name 'smallest_num' is not defined".
I have dinked around quite a bit and tried adding smallest_num = min(num1, num2, num3) however nothing has yielded a passing output.
The issue is that input() returns a string. So when you compare your variables, you're doing string comparisons instead of numerical comparisons. So, you need to convert your input to integers.
num1 = int(input("Enter num1: "))
num2 = int(input("Enter num2: "))
num3 = int(input("Enter num3: "))
print(min(num1, num2, num3))
This should work. I use C++, but it also looks like you didn't define smallest_num as a variable which could be the only problem.
if (num1 < num2 and num1 < num3):
small = num1
elif (num2 < num1 and num2 < num3):
small = num2
else:
small = num3
print(small)
In case anyone else needed help on this one in the future. :)
Original question: Write a program whose inputs are three integers, and whose output is the smallest of the three values.
If the input is:
7
15
3
The output is: 3
num1 = int(input())
num2 = int(input())
num3 = int(input())
if (num1 < num2 and num1 < num3):
small = num1
elif (num2 < num1 and num2 < num3):
small = num2
else:
small = num3
print(small)
num1 = int(input())
num2 = int(input())
num3 = int(input())
if (num1 <= num2 and num1 <= num3):
small = num1
elif (num2 <= num1 and num2 <= num3):
small = num2
else:
small = num3
print(small)
I am currently doing this lab for my own homework, and one thing that is missing from their responses is the definition of the variable that specifies which of the three values is the lowest/minimum value: lowest = min(num1, num2, num3)
num1 = int(input())
num2 = int(input())
num3 = int(input())
lowest = min(num1, num2, num3)
if num1 < num2 and num1 < num3:
lowest = num1
elif num2 < num1 and num2 < num3:
lowest = num2
elif num3 < num1 and num3 < num2:
lowest = num3
print(lowest)
This is supposed to take three int values, and give you the LOWEST amount
num1 = int(input())
num2 = int(input())
num3 = int(input())
if (num1 < num2) and (num1 < num3):
print(num1)
elif num2 < num3
print(num2)
else:
print(num3)
I know it's messy and kinda hard to read but my point is when i put something like 7 15 3, it always outputs 15. Why?
I reprodcued papke's result: adding a colon to the end of the elif line fixed the syntax error and produced the output you expected.
Taking advantage of the built-in min function, you could refactor your code along these lines:
num1 = int(input())
num2 = int(input())
num3 = int(input())
print(min([num1, num2, num3]))
You were missing a colon after the elif. The code gives the expected answer:
num1 = int(input("Enter: "))
num2 = int(input("Enter: "))
num3 = int(input("Enter: "))
if (num1 < num2) and (num1 < num3):
print(num1)
elif num2 < num3:
print(num2)
else:
print(num3)
How can I understand detailed difference between below two codes written to find the largest number among three numbers.
Code1:
def max_num(num1, num2, num3):
if num1 >= num2 and num1 >= num3:
return num1
elif num2 >= num1 and num2 >= num3
return num2
else:
return num3
Code2:
def max_num(num1, num2, num3):
if num1 > num2 and num1 > num3:
return num1
elif num2 > num1 and num2 > num3
return num2
else:
return num3
EDIT: Code2 could erroneously return num3 as max if num1 == num2, ie in [6, 6, 5].
The main difference, as commented above, is the use of "greater than" vs "greater or equal". I think the only place this would matter is when you have multiple values in the list which are equal, in which case Code2 would return the first instance, whereas Code3 would return the last.
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 6 years ago.
I'm trying to make a function where it checks to see if a number gets repeated 2 times and 3 times and when it does it will return either 0 (no repeats detected) or 1 (both arguments entered have repeats). But for someone reason, 1 always prints even when there are no repeats. Is there an easier way to do this or a way to fix my code? Also ignore the print('t'), print("w"), and print("x"). Those were just a way to check what was going on.
def triple_double(num1, num2):
num1 = str(num1)
num2 = str(num2)
if ('111') or ('222') or ('333') or ('444') or ('555') or ('666') or ('777') or ('888') or ('999') in num1:
if ('11') or ('22') or ('33') or ('44') or ('55') or ('66') or ('77') or ('88') or ('99') in num2:
print('t')
print(1)
else:
print("w")
print(0)
else:
print("x")
print(0)
triple_double(0, 0)
It should be:
def triple_double(num1, num2):
num1 = str(num1)
num2 = str(num2)
if ('111') in num1 or ('222') in num1 or ('333') in num1 or ('444') in num1 or ('555') in num1 or ('666') in num1 or ('777') in num1 or ('888') in num1 or ('999') in num1:
if ('11') in num2 or ('22') in num2 or ('33') in num2 or ('44') in num2 or ('55') in num2 or ('66') in num2 or ('77') in num2 or ('88') in num2 or ('99') in num2:
print('t')
print(1)
else:
print("w")
print(0)
else:
print("x")
print(0)
triple_double(0, 0)
Otherwise the if conditions will always evaluate to True.
I would just flip it around and test if either of the string representations of the numbers are in a list containing your triple and doubles.
from itertools import repeat
def triple_double(num1, num2):
# create triple and double lists
triples = [''.join(repeat(str(i), 3)) for i in list(range(1, 10))]
doubles = [''.join(repeat(str(i), 2)) for i in list(range(1, 10))]
num1 = str(num1)
num2 = str(num2)
# test if num1 in triples
if num1 in triples:
# test in num2 in doubles
if num2 in doubles:
print('t')
print(1)
else:
print("w")
print(0)
else:
print("x")
print(0)
triple_double(0, 0) # x, 0