我已经做了一个简单的功能,根据您决定运行的数字打印出时间表图表.由于我对该语言的基本理解,我遇到的问题是为什么它只返回第一个循环而没有别的.
def timestables(number):
for a in range(1, number+1):
b = a*a
c = a
return (str(c) + " * " + str(c) + " = " + str(b))
print(timestables(5))
Run Code Online (Sandbox Code Playgroud)
我得到答案..
1 * 1 = 1
Run Code Online (Sandbox Code Playgroud)
我试图通过使用print而不是return来纠正这个问题,但这最终会导致出现None.
def timestables(number):
for a in range(1, number+1):
b = a*a
c = a
print (str(c) + " * " + str(c) + " = " + str(b))
print(timestables(5))
Run Code Online (Sandbox Code Playgroud)
我得到答案..
1 * 1 = 1
2 * 2 = 4
3 * 3 = 9 …Run Code Online (Sandbox Code Playgroud) 我有一个程序,它有一个我希望访问的嵌套列表,然后根据条件附加到新列表.每个列表中有三列,我希望知道如何单独访问它们.这是它目前的样子[['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I']].一个更好地解释这个问题的例子是,如果我想要第二列中的数据,那么我的新列表就会如此['B', 'E', 'H'].
这是我到目前为止,但我现在相当困难..
n = 0
old_list = [['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I']]
new_list = []
for a, sublist in enumerate(old_list):
for b, column in enumerate(sublist):
print (a, b, old_list[a][b])
if n == 0:
new_list.append(column[0])
if n == 1:
new_list.append(column[1])
if n == 2:
new_list.append(column[2])
print(new_list)
Run Code Online (Sandbox Code Playgroud)
我目前的输出..
0 0 A
0 1 B
0 2 C
1 0 D
1 1 …Run Code Online (Sandbox Code Playgroud) 我想弄清楚如何将整个方块变成空心方块.到目前为止,我尝试过的一些事情并不是很成功,因为我最终得到了一个相当扭曲的三角形!
这是我目前要组建广场的代码..
size = 5
for i in range(size):
print ('*' * size)
Run Code Online (Sandbox Code Playgroud)
运行时,这是结果..
*****
*****
*****
*****
*****
Run Code Online (Sandbox Code Playgroud)
我是否需要在大于3 时运行if或while语句size来指定条件?
我目前正在完成一个涵盖定义功能的练习,我遇到了将所有支持功能放入主要功能的问题.
这是代码原来看起来的样子..
fn = input('Enter filename: ')
f = open(fn)
f.readline() # Skip the first two lines
f.readline()
line = f.readline() # Line containing site info
stuff = line.split(',')
print('\nSite: {}.\n(lat, long) = ({}, {})'.format(stuff[0], stuff[3], stuff[4]))
# Now process body of file, accumulating monthly rainfalls.
f.readline()
f.readline()
f.readline()
f.readline()
f.readline()
f.readline()
line = f.readline()
stuff = line.split(',')
rfs = 12 * [0] # Rainfall totals for months
while len(stuff) > 1:
date = stuff[1]
m = int(date[4:6]) # Month …Run Code Online (Sandbox Code Playgroud)