Discuss / Python / 交作业

交作业

Topic source

土城剑客

#1 Created at ... [Delete] [Delete and Lock User]

这个程序才是对的:

# -*- coding: utf-8 -*-

L = ['Bart', 'Lisa', 'Adam']

for name in L:
    print("Hello,%s!" % name)

输出结果:

Hello,Bart! Hello,Lisa! Hello,Adam!

下面这种方法其实是错误的:

for name in L:
     print('hello,',name,'!')

因为输入结果:

hello, Bart ! hello, Lisa ! hello, Adam !

因为逗号,在做拼接时会自动插入一个空格,所以不能用拼接的方法,应该使用格式化输出。 "hello,%s!" % name

Hjingxiang

#2 Created at ... [Delete] [Delete and Lock User]

用这个‘+’号就了呢。。 for name in L: print('hello,'+name+'!')


  • 1

Reply