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
Related
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 question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 1 year ago.
def max(num1,num2,num3):
if num1 > num2 and num1 > num3:
return num1
elif num2 > num1 and num2 > num3:
return num2
elif num3 > num1 and num3 > num2:
return num3
elif num1 == num2 == num3:
return num1 == num2 == num3
else:
print("something went wrong.")
print(max(4,5,8))
I am a basic python programmer learning python,i have tried to define a function which prints out maximum of a three number,but how to use the input function to get the number as input.
you can just use something like
we are using int function because input function takes the number or string and makes it a string so we need integer version of them so we can compare them
num1 = int(input("what is your first number: "))
num2 = int(input("what is your second number: "))
num3 = int(input("what is your third number: "))
but it can give errors because you did not use try except so the proper way is
while True:
try:
num1 = int(input("what is your first number: "))
num2 = int(input("what is your second number: "))
num3 = int(input("what is your second number: "))
except:
print("give me integer or number or i do not know what type of data do you want")
else:
break
you can change what is in the last print statement
you can use that
it is still impovable you should check assert
i hope it is helpful
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.
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)