x = int(input('Enter x: '))
y = int(input('Enter y: '))
product = 0
## 50 in border
border = str('-------------------------------------------------------')
print(border)
print('%15s' % 'x', '%15s' % 'y', '%23s' % 'product')
print(border)
if y < x:
temp = x
x = y
y = temp
xTwo = x
yTwo = y
productTwo = x*y
while not x <= 0:
if x % 2 == 0:
x = x / 2
y = y*2
else:
x = x - 1
product = product + y
print('%15d' % x, '%15d' % y, '%23d' % int(product))
else:
print(border)
print(xTwo, '*', yTwo, '=', productTwo)
**以下是它的产生。在循环中,我希望第一个数字行读取&#39; 12 14 0&#39;但是我不确定我需要在哪里放置打印线,或者我是否可以稍微改变循环以产生结果****
Enter x: 12
Enter y: 14
-------------------------------------------------------
x y product
-------------------------------------------------------
6 28 168
3 56 168
2 56 224
1 112 224
0 112 336
-------------------------------------------------------
12 * 14 = 168
答案 0 :(得分:0)
问题在于print语句的位置。您需要将它放在while循环的第一行。如果不这样做,则x%2 = 0条件变为真,x值改变。
x = int(input('Enter x: '))
y = int(input('Enter y: '))
product = 0
## 50 in border
border = str('-------------------------------------------------------')
print(border)
print('%15s' % 'x', '%15s' % 'y', '%23s' % 'product')
print(border)
if y < x:
temp = x
x = y
y = temp
xTwo = x
yTwo = y
productTwo = x*y
while not x <= 0:
print('%15d' % x, '%15d' % y, '%23d' % int(product))
if x % 2 == 0:
x = x / 2
y = y*2
else:
x = x - 1
product = product + y
else:
print(border)
print(xTwo, '*', yTwo, '=', productTwo)