So this question isn't hard by any means but just worded in a confusing way. I've tried 3 solutions, and none of them are correct. I've given my genuine effort, so I hope someone else can help me out here. Here's the question:
Using a
for
loop, write the following program: all the two-digit numbers which are equal to double the product of the digits that make up the given number are displayed in a column."
Here are the 3 solutions I've tried so far:
1.
num = int(input("Enter a two-digit number: "))
digitArr = []
for i in str(num):
digitArr.append(i)
result = 1
for j in digitArr:
result = result * int(j)
result = result ** 2 #here
for i in range(10, 100):
tempDigitArr = []
for j in str(i):
tempDigitArr.append(j)
tempResult = 1
for k in tempDigitArr:
tempResult = tempResult * int(k)
tempResult = tempResult ** 2 #here
if tempResult == result:
print(i)
tempResult = 1
continue
else:
tempResult = 1
continue
num = int(input("Enter a two-digit number: "))
digitArr = []
for i in str(num):
digitArr.append(i)
result = 1
for j in digitArr:
result = result * int(j)
result = result ** 2 #here
for i in range(10, 100):
tempDigitArr = []
for j in str(i):
tempDigitArr.append(j)
tempResult = 1
for k in tempDigitArr:
tempResult = tempResult * int(k)
tempResult = tempResult ** 2 #here
if tempResult == result:
print(i)
tempResult = 1
continue
else:
tempResult = 1
continue
num = int(input("Enter a two-digit number: "))
digitArr = []
for i in str(num):
digitArr.append(i)
result = 1
for j in digitArr:
result = result * int(j)
for i in range(10, 100):
tempDigitArr = []
for j in str(i):
tempDigitArr.append(j)
tempResult = 1
for k in tempDigitArr:
tempResult = tempResult * int(k)
if tempResult == result:
print(i)
tempResult = 1
continue
else:
tempResult = 1
continue
And yes, if anyone's wondering; my course is using an auto-check system ;-;
CodePudding user response:
The methodology is pretty straightforward
test = range(10,99)
for num in test:
prod = 1
for digit in str(num): prod = prod * int(digit)
if num == 2*prod:
print(num)
Fun fact: only 36 qualifies for this requirement
CodePudding user response:
IIUC, you want:
for i in range(10, 100):
tens, units = divmod(i, 10)
if tens*units*2 == i:
print(i)
CodePudding user response:
I'm not giving you code (others did so already anyway) but rather explain what (I think) is wrong with your program:
- you say the program is auto-graded, but the question does not say anything about input, so I guess that by expecting
input
your program just waits forever for input that is never provided; instead you probably just need thefor num in range(10, 100)
loop at the bottom - if you do the same calculation twice (which you do not need, see above, but if), you should define a function using
def
, not copy the code - the question asks about the number being double the product of its digits, not the square, i.e.use
*2
, not**2
- and you should compare that double-product to the number itself, not to the product of that first (not asked for) input-number
- since you know it's a two-digit number, you do not actually have to loop over the digits but can access them directly using integer(!)-division
//
and modulo%
I'm sure you'll get it right with those hints (and without just copying the full code).
CodePudding user response:
If I understand the requirement correctly, the given number is to be obtained from the user and the two digit numbers have to match it (not themselves):
number = int(input("given number: "))
for n in range(10,100):
d,u = divmod(n,10)
if d*u*2 == number:
print(n)
Sample run:
given number: 36
29
36
63
92