python学习1——第一个程序“print”。

今天开始记录学习python过程中遇到的问题和并做一些笔记。

一、第一个程序。

第一个学习的命令是print。

 

利用IDLE写命令时,每写完一次命令回车时,命令会被立即执行。例:

>>> print("Well I wonder, could it be")

Well I wonder, could it be

>>> print("when I was dreaming about you baby, you were dreaming of me")

when I was dreaming about you baby, you were dreaming of me

>>> print("Did I lose my love to someone better?")

Did I lose my love to someone better?

>>> print("Did she love you like I do?")

Did she love you like I do?

>>> print("I do you know I really really do")

I do you know I really really do

保存到E盘 建立新的文件夹“abc”,保存文件py1.py。

1、利用cmd调用这几个命令时:

C:\Users\Shannon_V>python py1.py
(null): can't open file 'py1.py': [Errno 2] No such file or directory

报错1:

(null): can't open file 'py1.py': [Errno 2] No such file or directory

解决方法:

需要到指定的保存路径即E盘/abc文件夹下调用:

C:\Users\Shannon_V>E:

E:\>cd E:abc

E:\abc>python py1.py
File "py1.py", line 1
>>> print("Well I wonder, could it be")
^
SyntaxError: invalid syntax

报错2:

  File "py1.py", line 1

    >>> print("Well I wonder, could it be")

     ^

SyntaxError: invalid syntax

解决方法:

不能直接调用IDLE保存的文件,因为这个文件里包括了命令行和已经执行了命令,可以将命令单独粘贴只文本编辑器(写字板也可以,保存时应保存为py文件),例:

print("Well I wonder, could it be")

print("when I was dreaming about you baby, you were dreaming of me")

print("Did I lose my love to someone better?")

print("Did she love you like I do?")

print("I do you know I really really do")

只保留命令即可打开:

C:\Users\Shannon_V>E:

E:\>cd E:abc

E:\abc>python py1.py

Well I wonder, could it be
when I was dreaming about you baby, you were dreaming of me
Did I lose my love to someone better?
Did she love you like I do?
I do you know I really really do
How do I carry on?
The day you went away
please enter any key to exit...

同样,如果直接利用python打开由IDLE保存的py文件,会出现闪退的情况,此时也需要将命令行并且只将命令行单独保存成py文件,即可双击文件,利用python打开命令。

2、将脚本都打印在同一行。

在输入所需要打印的文本后面加上end=””,即可将文本打印在同一行。

print("si",end="")

print("cudsihi",end="")

print("Did she love you like I do?",end="")

print("I do you know I really really do",end="")

 总结:

由于完全没有任何计算机基础,所以在以上几个问题中摸索了很久。现小节如下:

1、python文件应只保存命令行。

2、需要到指定的文件夹下才能成功调用。

3、利用end=“”可以让脚本都打印在同一行。

posted on 2018-08-16 16:34  shannon_V  阅读(175)  评论(1)    收藏  举报

导航