Discuss / Python / python只能执行一条命令

python只能执行一条命令

伍伍詹

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

本人新手第一次接触python,遇到一个问题自己尝试过各种方法后依然无法解决,不知道环境搭建的时候哪里出错了。按照教程指导安装python2.7.3之后,每次只能读取一条命令,第二个语句便无法读取了。for循环无法执行,具体代码跟反应如下:

Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> print 'I like to use the Internet for:'
for item in ['e-mail','net-surfing','homework','chat']:
    print item,
print
I like to use the Internet for:
>>> 
>>>

没有错误提示,但是第二条语句开始就没有执行了。 下面的语句也跟期望的不一样

>>> age = 20
if age >= 18:
    print 'your age is', age
    print 'adult'

>>> 
>>> print age
20
>>>

第一个语句'age = 20'有执行,但是从第二个开始就又无法执行了。第一次接触python,各种搜索引擎都搜过了,没有找到解决方法,自己尝试过不少方法,实在解决不了,只好发帖提问,希望大神帮帮忙,十分感谢。

伍伍詹

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

第二行打错,安装的是python2.7.3版本。

伍伍詹

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

2.7.6,这笔误……

盖山

#4 Created at ... [Delete] [Delete and Lock User]
你可以封装在一个函数里, Python 解释器里执行代码和单个文件是不同的
>>> age = 20
>>> # def 定义一个函数, 传入参数 age
>>> def print_age(param):
...     print age
...     if age >= 18:
...         print 'your age is: ', age
...         print 'adult'
... 
>>> print_age(age)
20
your age is:  20
adult

或者

>>> def print_age():
...     age = 20
...     print age
...     if age >= 18:
...         print 'your age is: ', age
...         print 'adult'
... 
>>> print_age()
20
your age is:  20
adult

伍伍詹

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

十分感谢!!看来我得跳过《python核心编程》(第二版)第二章快速入门……


  • 1

Reply