pythonedx

I cannot get past this stage of my problem: what's wrong? edx


mystery_int = 3
#Write a program that will print the times table for the
#value given by mystery_int. The times table should print a
#two-column table of the products of every combination of
#two numbers from 1 through mystery_int. Separate consecutive
#numbers with either spaces or tabs, whichever you prefer.
#
#For example, if mystery_int is 5, this could print:
#
#1  2   3   4   5
#2  4   6   8   10
#3  6   9   12  15
#4  8   12  16  20
#5  10  15  20  25
#
#To do this, you'll want to use two nested for loops; the
#first one will print rows, and the second will print columns
#within each row.
#
#Hint: How can you print the numbers across the row without
#starting a new line each time? With what you know now, you
#could build the string for the row, but only print it once
#you've finished the row. There are other ways, but that's
#how to do it using only what we've covered so far.
#
#Hint 2: To insert a tab into a string, use the character
#sequence "\t". For example, "1\t2" will print as "1    2".
#
#Hint 3: Need to just start a new line without printing
#anything else? Just call print() with no arguments in the
#parentheses.
allnums = ("")
colcount = 1
rowcount = 1
for i in range(1, mystery_int):
    for i in range(1, mystery_int * rowcount):
        allnums += str(rowcount * colcount)
        allnums += "\t"
        rowcount += 1
    colcount += 1
print(allnums)

What's wrong? First I want to try to do it in a single string, then in a table. Also, I don't see what hint 3 is for. Why would you need to print nothing This is my output: 1 2


Solution

  • Thanks, Asriel. However, the code asked for something different so I made some minor edits. This is the edited version

    mysteryList=[]
    for i in range(1,int(mystery_int)+1):
     mysteryList.append(str(i) + "   ")
    print(" ".join(mysteryList))
    for j in range(1,len(mysteryList)):
     rowList=[str(j+1)] 
     for k in range(1,len(mysteryList)):
      rowList.append(str(int(mysteryList[k])*int(mysteryList[j])))
     print("    ".join(rowList))