catNames = []
while True:
print('Enter the name of cat' + str(len(catNames) + 1) + ' (Or enter nothing to stop.):')
name = input()
if name=='':
break
catNames = catNames + [name]
print('The cat names are:')
for name in catNames:
print(''+ name)
和函数一回事,只是它是调用在一个值上
index()方法用于判断传入的值是否在列表中,有则返回它第一次出现时(若列表中重复出现)的下标,没有则报错
import random
message = ['It is certain',
'It is decidedly so',
'Yes definitely',
'Reply hazy try again',
'Ask again later',
'Concentrate and ask again',
'My reply is no',
'Outlook not so good',
'Very doubtful']
print(message[random.randint(0,len(message) - 1)])
【改变字符串的正确方式】切片+连接
def eggs(sth):
sth.append('hello')
spam=[1,2,3]
eggs(spam)
print(spam)
def strcat(spam):
spam.insert(len(spam)-1, 'and')
string = ''
for i in range(0,len(spam)-2):
string += spam[i]
string += ','
string += spam[len(spam)-2]
string += ' '
string += spam[len(spam)-1]
return string
spam=['apples','bananas','tofu','cats','pig']#可修改为其他列表
print(strcat(spam))
def picture(grid):
for i in range(0,len(grid[0])):
for j in range(0,len(grid)):
print(grid[j][i],end=' ')
print('\n')
grid = [['.','.','.','.','.','.'],
['.','0','0','.','.','.'],
['0','0','0','0','.','.'],
['0','0','0','0','0','.'],
['.','0','0','0','0','0'],
['0','0','0','0','0','.'],
['0','0','0','0','.','.'],
['.','0','0','.','.','.'],
['.','.','.','.','.','.'],]
picture(grid)