设为首页 加入收藏

TOP

python学习第一天 -----2019年4月15日(三)
2019-05-23 16:08:28 】 浏览:71
Tags:python 学习 第一 -----2019年 4月 15日
t;"
name = "你好,世界"
print(name)
======================================================================

G:\Python2.7.5\python.exe G:/practise/oldboy/day1/HelloWorld.py
File "G:/practise/oldboy/day1/HelloWorld.py", line 24
SyntaxError: Non-ASCII character '\xe4' in file G:/practise/oldboy/day1/HelloWorld.py on line 24, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

Process finished with exit code 1

原因:python2中因为没有指定字符编码集,所以报错

#!/usr/bin/env python 
#-*- coding:utf-8 _*-

name = ("你好,世界").decode(encoding="utf-8")
print(name)
========================================================================

G:\Python2.7.5\python.exe G:/practise/oldboy/day1/HelloWorld.py
你好,世界

Process finished with exit code 0

解决方法:导入utf-8字符集(#-*- coding:utf-8 _*-)并解码  decode(encoding="utf-8")

在puthon 3中

#!/usr/bin/env python 

"""
@author:chenjisong
@file: HelloWorld.py
@time: 2019/04/15
url:https://www.liaoxuefeng.com
functions:
Software:JetBrains PyCharm 4.5.3
"""

name = "你好,世界"
print(name)
========================================================================

G:\Python3.7.3\python.exe G:/practise/oldboy/day1/HelloWorld.py
你好,世界

Process finished with exit code 0

在python 3中无需指定编码格式也无需解码

第一周-第10章节-Python3.5-用户交互程序

#!/usr/bin/env python 
#-*- coding:utf-8 _*-
"""
@author:chenjisong
@file: interaction.py
@time: 2019/04/15
url:https://www.liaoxuefeng.com
functions:
Software:JetBrains PyCharm 4.5.3
"""
username=input("username:")
password=input("Password:")
print("Username is "+username,"and Password is "+password)
===========================================================================

G:\Python3.7.3\python.exe G:/practise/oldboy/day1/interaction.py
username:chenjisong
Password:chenjisong
Username is chenjisong and Password is chenjisong

Process finished with exit code 0

解释:红色部分为用户输入的部分。返回的结果调用了输入的变量,形成了交互程序

字符串拼接第一种方法(占位符):

#!/usr/bin/env python 
#-*- coding:utf-8 _*-
"""
@author:chenjisong
@file: interaction.py
@time: 2019/04/15
url:https://www.liaoxuefeng.com
functions:
Software:JetBrains PyCharm 4.5.3
"""
Name=input("name:")
Age=input("Age:")
Job=input("Job:")
Salary=input("Salary:")
info='''
----------------info of %s------------------
Name:%s
Age:%s
Job:%s
Salary:%s
''' % (Name,Name,Age,Job,Salary)
print(info)
=========================================================================

G:\Python3.7.3\python.exe G:/practise/oldboy/day1/interaction.py
name:chenjisong
Age:23
Job:IT
Salary:3000

----------------info of chenjisong------------------
Name:chenjisong
Age:23
Job:IT
Salary:3000


Process finished with exit code 0

注意:%s代表字符串

            %d代表整数类型

            %f代表浮点数

字符串拼接第二种方法(字符串转换):

#!/usr/bin/env python 
#-*- coding:utf-8 _*-
"""
@author:chenjisong
@file: interaction.py
@time: 2019/04/15
url:https://www.liaoxuefeng.com
functions:
Software:JetBrains PyCharm 4.5.3
"""
Name=input("name:")
Age=int(in
首页 上一页 1 2 3 4 5 6 下一页 尾页 3/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇周末学习笔记——day01(函数,函.. 下一篇数据结构(三):队列

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目