我有一個學校練習,但我很難理解如何做乘法表。有人可以幫我嗎?

因此,根據數字和列數,乘法表必須不同。我試圖以這種方式解決這個問題:
number1 = 4
column1 = 3
for row in range(column1):
for y in range(10):
print("0" str(number1) " " "x" "0" "")
但我不知道在列印陳述句中放什么。有人可以解釋一下如何解決這個練習嗎
uj5u.com熱心網友回復:
你可以試試這樣的
def multiplication_table(num, columns):
output = ""
for i in range(1, 10):
output = f"{num} x {i} = {str(num * i).zill(2)}"
if i % columns == 0:
output = "\n"
else:
output = "\t"
return output
print(multiplication_table(8, 2))
輸出 -
8 x 1 = 08 8 x 2 = 16
8 x 3 = 24 8 x 4 = 32
8 x 5 = 40 8 x 6 = 48
8 x 7 = 56 8 x 8 = 64
8 x 9 = 72
uj5u.com熱心網友回復:
如果你把它分解成它的組成部分,它會更容易理解。
首先構建一個字串串列,其中每個字串代表乘法表中的一個“單元格”。
一旦你有了它,你就可以處理輸出格式。
像這樣的東西:
def mtable(x, c):
vlist = []
for i in range(1, 10):
vlist.append(f'{x:02d} x {i:02d} = {x*i:02d}')
for i in range(0, 10, c):
print('\t'.join(vlist[i:i c]))
mtable(4, 3)
輸出:
04 x 01 = 04 04 x 02 = 08 04 x 03 = 12
04 x 04 = 16 04 x 05 = 20 04 x 06 = 24
04 x 07 = 28 04 x 08 = 32 04 x 09 = 36
uj5u.com熱心網友回復:
你需要一些東西來輸入數字:
def get_number(text,rng):
"""Ask with 'text' until the given input is int()-able and
inside range 'rng'. Return the integer - present error and
loop if not an int input or outside of specified."""
while True:
try:
t = input(text)
n = int(t)
if n in rng:
return n
print("Invalid number, use integer in", rng)
except ValueError:
print("Invalid number, use integer in", rng)
您需要一些東西來創建表格 - 字串連接是浪費的,因此請改用串列并將'--'.join("1","3"])它們連接回字串。
要格式化數字,請使用 pythons Format Specification Mini-Language在字串中右對齊和填充數字:
def mult_table(num, times, cols):
out = [[]]
# calculate max width for spacing reasons
maxw =len(str(num*times))
for i in range(1, times 1):
# add to last list inside out
# format numbers right aligned '>' with leading '0' to 'maxw' digits
out[-1].append( f"{num:>0{maxw}} x {i:>0{maxw}} = {num * i:>0{maxw}}")
# if out's last list reached length you need a new line, add new []
if len(out[-1])==cols:
out.append([])
# create the texts from your list of lists
return '\n'.join(' '.join(e) for e in out)
然后你需要得到一些輸入:
n = get_number("What table? ", range(1,21))
t = get_number("Up to what multiplyer? ", range(1,51))
c = get_number("How many columns? ", range(1, n 1))
print(mult_table(n, t, c))
要得到:
What table? 8
Up to what multiplyer? 12
How many columns? 3
08 x 01 = 08 08 x 02 = 16 08 x 03 = 24
08 x 04 = 32 08 x 05 = 40 08 x 06 = 48
08 x 07 = 56 08 x 08 = 64 08 x 09 = 72
08 x 10 = 80 08 x 11 = 88 08 x 12 = 96
What table? 13
Up to what multiplyer? 42
How many columns? 7
013 x 001 = 013 013 x 002 = 026 013 x 003 = 039 013 x 004 = 052 013 x 005 = 065 013 x 006 = 078 013 x 007 = 091
013 x 008 = 104 013 x 009 = 117 013 x 010 = 130 013 x 011 = 143 013 x 012 = 156 013 x 013 = 169 013 x 014 = 182
013 x 015 = 195 013 x 016 = 208 013 x 017 = 221 013 x 018 = 234 013 x 019 = 247 013 x 020 = 260 013 x 021 = 273
013 x 022 = 286 013 x 023 = 299 013 x 024 = 312 013 x 025 = 325 013 x 026 = 338 013 x 027 = 351 013 x 028 = 364
013 x 029 = 377 013 x 030 = 390 013 x 031 = 403 013 x 032 = 416 013 x 033 = 429 013 x 034 = 442 013 x 035 = 455
013 x 036 = 468 013 x 037 = 481 013 x 038 = 494 013 x 039 = 507 013 x 040 = 520 013 x 041 = 533 013 x 042 = 546
# handles bad input gracefully
What table? apple
Invalid number, use integer in range(1, 21)
What table? 42
Invalid number, use integer in range(1, 21)
What table?
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/467853.html
上一篇:根據回圈值將專案添加到串列c#
