我无法克服我的问题的这个阶段:怎么了?埃德克斯
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)
怎么了?首先,我想尝试在单个字符串中完成,然后在 table 中完成。
另外,我看不到提示 3 的用途。为什么你不需要打印任何东西
这是我的输出:
1 2
恐怕我不明白你的代码到底发生了什么,但我认为这是一个更好的方法:
#We ask the user to enter misteryNumber...
misteryNumber=input("Welcome, please type the mistery number: ")
#Then we generate a list of numbers which goes from 1 to the mistery number.
misteryList=[]
for i in range(1,int(misteryNumber)+1):
misteryList.append(str(i))
#We print the output...
print("Here are the results!")
print("-----------------------------------")
#The first row is always going to be misteryList.
print(" ".join(misteryList))
#After that we create a list for every low in wich we store its values...
for j in range(1,len(misteryList)):
#We reset the list for each row with the first member of the list being the row number.
rowList=[str(j+1)]
#A nested for loop to calculate each value within a row...
for k in range(1,len(misteryList)):
rowList.append(str(int(misteryList[k])*int(misteryList[j])))
#We print the new row...
print(" ".join(rowList))
这是我用数字 5 运行 时它给我的输出:
Welcome, please type the mistery number: 5
Here are the results!
-----------------------------------
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
PD:我也不明白第三个提示的要点,每次使用 print
时 python 都会自动添加新行(至少在 python 3 中)。
谢谢,Asriel。然而,代码要求不同的东西,所以我做了一些小的修改。这是编辑后的版本
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))
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)
怎么了?首先,我想尝试在单个字符串中完成,然后在 table 中完成。 另外,我看不到提示 3 的用途。为什么你不需要打印任何东西 这是我的输出: 1 2
恐怕我不明白你的代码到底发生了什么,但我认为这是一个更好的方法:
#We ask the user to enter misteryNumber...
misteryNumber=input("Welcome, please type the mistery number: ")
#Then we generate a list of numbers which goes from 1 to the mistery number.
misteryList=[]
for i in range(1,int(misteryNumber)+1):
misteryList.append(str(i))
#We print the output...
print("Here are the results!")
print("-----------------------------------")
#The first row is always going to be misteryList.
print(" ".join(misteryList))
#After that we create a list for every low in wich we store its values...
for j in range(1,len(misteryList)):
#We reset the list for each row with the first member of the list being the row number.
rowList=[str(j+1)]
#A nested for loop to calculate each value within a row...
for k in range(1,len(misteryList)):
rowList.append(str(int(misteryList[k])*int(misteryList[j])))
#We print the new row...
print(" ".join(rowList))
这是我用数字 5 运行 时它给我的输出:
Welcome, please type the mistery number: 5
Here are the results!
-----------------------------------
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
PD:我也不明白第三个提示的要点,每次使用 print
时 python 都会自动添加新行(至少在 python 3 中)。
谢谢,Asriel。然而,代码要求不同的东西,所以我做了一些小的修改。这是编辑后的版本
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))