
這是程式的圖片/它應該如何作業,它應該顯示 1*1 然后 12$3 < 3 是 1 2 的總和 .. 我們只需要 for 回圈。
我已經嘗試了很多解決方案,這就是我最后得到的,由于某種原因,我無法在不洗掉代碼的情況下將其復制并粘貼到此處。
當前的輸出是:
請幫助,非常感謝
uj5u.com熱心網友回復:
我會這樣實作這個想法:
def func(n: int):
for i in range(1, n 1):
nsum = 0 # Sum of numbers in nested loop.
last = 0 # Last number added to 'nsum'.
string = '' # Final string which is then printed.
for j in range(1, i 1):
nsum = j # Add to total sum.
string = str(j) # Add to final string.
last = j # Set last number to current.
# Decide if either asterisk (*) or dollar ($) should be included
# in final message, after it append total sum.
string = ('*' if last % 2 else '$') str(nsum)
print(string)
func(6)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/529480.html
