免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2609 | 回复: 3
打印 上一主题 下一主题

学了五天了,求教关于FOR语句的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-03-22 00:00 |只看该作者 |倒序浏览
菜鸟上路,
求教:
#!/usr/bin/python
#Filename:first.py
print  'this  is  my first  program  ,please  gei me some  congralations  ,thank  you  !!!'
print  'now  ,i  want to  gei you  a  question  ,please gei  me  an answer  .'
for  i  in  range(1,8,1):
        print  'this  is ',i,'line'
        for r in range(1,i,1):
                print  r
print 'it is over '
像上面的程序,我想要的效果是
1
12
123
1234
12345
123456
1234567
12345678

可是为什么总是不行呢?上面的执行结果是
#!/usr/bin/python
this  is  my first  program  ,please  gei me some  congralations  ,thank  you  !!!
now  ,i  want to  gei you  a  question  ,please gei  me  an answer  .
this  is  1 line
this  is  2 line
1
this  is  3 line
1
2
this  is  4 line
1
2
3
this  is  5 line
1
2
3
4
this  is  6 line
1
2
3
4
5
this  is  7 line
1
2
3
4
5
6
it is over
请问为什么总是少一行呢 ?为甚8那行总是没有呢?第一二行怎们那样呢?
还有就是怎么不让它换行啊,
我采学两三天,别怪我太笨哦

还有就是这个哦
#!C:\Python24\python.exe
#Filename:l.py
number=23
value=1
while value:
    try:
        guess=int(raw_input('Enter an interger :'))
    except ValueError:
        print 'sorry ,please input a new number ,thank you .'
        continue
    else:
        value=0
if guess==number:
    print 'Congratulation to you ,you guessed it ,you are very good .'
    print '(but you do not win any prizes !)'
elif guess<number:
    print 'No ,sorry ,it is a little lower than that .'
else:
    print 'No ,sorry ,it is a little highter than that '
print 'done'
print ' '
print ' '
print ' '
就是对数据的输入错误处理,而要求必须输入整数的时候,我那样处理是不是太麻烦了啊?
谢谢了,谢谢了哦,想不出什么别的方法了啊
3751 该用户已被删除
2 [报告]
发表于 2006-03-22 08:38 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
3 [报告]
发表于 2006-03-22 09:18 |只看该作者
一个print后面会换行,

print 1,

后面加逗号的话,不会换行,但有一个空格,因此象3751所说,或合成一个字符串一起输出

建议你单独运行range试一试,它的取值是:

range(x, y, n)

x <= i < y

所以不包含后面的y。如果步长为1,可以省略n

论坛徽章:
0
4 [报告]
发表于 2006-03-22 22:57 |只看该作者
哦,我试过了,确实是那样的啊,谢谢你们了啊,我就在看简明教程啊,只是边看边写程序啊,所以只看到RANGE了,不知道其确切的含意呢,呵呵,谢谢了啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP