免费注册 查看新帖 |

Chinaunix

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

刚学python,实验书上的2个小程序遇到的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-09-16 14:10 |只看该作者 |倒序浏览
第一个:  if 语句,完全按照书上的书写

#!/usr/bin/python
# Filename: if.py

number = 23
guess = int(input('Enter an integer : '))

if guess == number:
    print('Congratulations, you guessed it.') # New block statrs here
        print('(but you do not win any prizes!)') # New block ends here
elif guess < number:
    print('No, it is a little higher than that') # Another block
        # You can do whatever you want in a block ...
else:
    print('No, it is a little lower than that')
        # You must have guess > number to reach here
print('Done')

执行后报错:

root@ubuntu:~$ python if
  File "if", line 9
    print('(but you do not win any prizes!)') # New block ends here
    ^
IndentationError: unexpected indent


第二个: while语句
#!/usr/bin/python
# Filename while.py

number == 23
running = Ture

while running:
    guess = int(input("Enter a integer: '))
       
        if guess == number:
            print('Congratulations, you guessed it.')
                running = False #thi cause the wile loop to stop
        elif guess < number:
            print('No, it is a little higher than that.')
else:
    print('The while loop is over.')
        # Do anything else you want to do here
print('Done')

执行后报错:
root@ubuntu:~$ python while
  File "while", line 8
    guess = int(input("Enter a integer: '))
                                          ^
SyntaxError: EOL while scanning string literal


请各位高人指点。 多谢!

论坛徽章:
2
青铜圣斗士
日期:2015-11-26 06:15:59数据库技术版块每日发帖之星
日期:2016-07-24 06:20:00
2 [报告]
发表于 2010-09-16 16:24 |只看该作者
python缩进敏感, 所以

  1. if guess == number:
  2.     print('Congratulations, you guessed it.') # New block statrs here
  3.         print('(but you do not win any prizes!)') # New block ends here
复制代码
改为:

  1. if guess == number:
  2.     print('Congratulations, you guessed it.') # New block statrs here
  3.     print('(but you do not win any prizes!)') # New block ends here
复制代码
第2个: EOL(end of line) while scanning string literal
扫描string literal时遇见了行结束。

string literal的delimiter没有匹配:
int(input("Enter a integer: '))

论坛徽章:
0
3 [报告]
发表于 2010-09-16 17:37 |只看该作者
没看也楼上两个有啥区别

论坛徽章:
0
4 [报告]
发表于 2010-09-16 20:32 |只看该作者
本帖最后由 a515200 于 2010-09-18 00:29 编辑

~~~~~~~~~~~~~~~

论坛徽章:
0
5 [报告]
发表于 2010-09-17 09:35 |只看该作者
input() 可以输入字符串

论坛徽章:
0
6 [报告]
发表于 2010-09-18 14:55 |只看该作者
第一个,二楼说对了,不过,第二个,可不只这个问题。看下面:
  1. #!/usr/bin/python
  2. # Filename while.py

  3. number = 23
  4. running = True

  5. while running:
  6.     guess = int(input("Enter a integer: "))
  7.     if guess == number:
  8.         print('Congratulations, you guessed it.')
  9.         running = False #thi cause the wile loop to stop
  10.     elif guess < number:
  11.         print('No, it is a little higher than that.')
  12. else:
  13.     print('The while loop is over.')
  14.         # Do anything else you want to do here
  15. print('Done')
复制代码

论坛徽章:
0
7 [报告]
发表于 2010-09-18 14:57 |只看该作者
你的 number 不应该是 == 23,running 是True,不是Ture,还有注意缩进和单双引号对应。

论坛徽章:
0
8 [报告]
发表于 2010-09-19 13:00 |只看该作者
我学习python的最大感触就是不要用tab键缩进哦,去python社区看下“八荣八耻”吧!!

论坛徽章:
0
9 [报告]
发表于 2010-09-20 17:02 |只看该作者
回复 2# OwnWaterloo


    感谢指点!明白啦!

论坛徽章:
0
10 [报告]
发表于 2010-09-20 17:17 |只看该作者
你的 number 不应该是 == 23,running 是True,不是Ture,还有注意缩进和单双引号对应。
icykiss 发表于 2010-09-18 14:57



    非常感谢你的解答,我已经改正了缩进和拼写。现在遇到一个新的问题。

Traceback (most recent call last):
  File "while", line 4, in <module>
    number == 23
NameError: name 'number' is not defined


我要在number前定义number为什么类型吗? 书上为什么没写呀?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP