Python 输入/输出

程序要实现人机交互功能,需要能够向显示设备输出有关信息及提示,同时也要能够接收从键盘输入的数据。Python提供了用于实现输入/输出功能的函数input( )和输出函数

print(),下面分别对这两个函数进行介绍。

1.input( )函数

input()函数用于接收一个标准输入数据,该函数返回一个字符串类型数据,其语法格式如下:

input([prompt]) #参数说明:prompt提示信息

下面通过一个模拟用户登录的案例演示print()函数与input()函数的使用,具体如下:

user_name = input(‘请输入账号:’)

password = input(‘请输入密码:’)

print(‘登录成功!’)

程序运行结果:

Python 输入/输出

2. print()函数

print()函数用于向控制台中输出数据,它可以输出任何类型的数据,该函数的语法格式如下:

print( *objects,sep = ”,end = ‘\n’,file = sys.stdout )

print( )函数中各个参数具体含义如下:

(1)objects:表示输出的对象。输出多个对象时,需要用逗号分隔。

(2)sep:用于间隔多个对象。

(3)end: 用于设置以什么结尾。默认是换行符 \n。

(4)file:表示数据输出的文案对象。

下面通过一个打印名片的案例演示print( )函数的使用,具体如下:

print(‘姓名:小可爱’)

age = 13

print(‘年龄:’,age)

print(‘地址:甘肃庆阳’)

示例1:海洋单位距离的换算

在陆地上,参考可以用来确定两点之间的距离,使用厘米、米、公里等作为测量单位,而在海上则缺乏参考。人们将一分钟赤道经度对应的距离记录为一海里,并使用海里作为海上测量单位。公里和海里可以用以下公式换算:

[En]

On land, reference can be used to determine the distance between two points, using centimeters, meters, kilometers, etc., as units of measurement, while there is a lack of reference at sea. People record the distance corresponding to one minute of equatorial longitude as one nautical mile and use nautical miles as the unit of measurement at sea. Kilometers and nautical miles can be converted by the following formula:

1海里 = 1.852公里

本例需要一个将公里转换为海里的程序。

[En]

This example requires a program to convert kilometers into nautical miles.

kilometre = float(input(‘请输入公里数: ‘))

nautical_mile = (kilometre / 1.852)

print(‘换算后的海里数为:’, nautical_mile, “海里”)

运行结果如下:

Python 输入/输出

1.5 实例2:打印名片

名片是一张纸,它标识了一个名字及其组织、公司和联系方式。这也是新朋友相互了解和自我介绍的一种快速有效的方式。写一个程序。

[En]

A business card is a piece of paper that identifies a name and its organization, company and contact method. It is also a quick and effective way for new friends to get to know each other and introduce themselves. Write a program.

print(‘传智播客教育科技股份有限公司’)

print(‘张先生 主管’)

print(‘——————————–‘)

print(‘手机:18688888888’)

print(‘地址:北京昌平区建材城西路金燕龙办公楼’)

程序运行结果如下:

Python 输入/输出

Original: https://blog.51cto.com/u_15754660/5586201
Author: 香沫妃儿
Title: Python 输入/输出

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/500685/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球