haibaraxx的个人博客分享 http://blog.sciencenet.cn/u/haibaraxx

博文

Python3: 提示输入——input函数

已有 16720 次阅读 2017-5-31 03:42 |个人分类:Python|系统分类:科研笔记

input () ——提示输入,输入对象为string格式,返回值为string格式。


python脚本示例如下:


#!/usr/bin/python3 (注:#!后面的路径可通过在terminal中输入which python确定)

myname = input ('What is your name?')

print ('Hello ' + myname)

print (myname + ', Let's add two numbers')


a = input ('Enter the first number: ') # a 是string格式

b = input ('Enter the second number: ') # b 是string格式


cc = a + b

print (cc)

print ('The sum of a and b is ' + cc)


c = int(a) + int (b) # int () 将对象转化成整数格式

print (repr(c))

print ('The sum of a and b is ' + repr (c))


运行结果如下:

What is your name? (提示输入)YY

Hello YY

YY, Let's add two numbers

Enter the fitst number: (提示输入)2.3

Enter the fitst number: (提示输入)2

'2.32'

The sum of a and b is 2.32

'4'

The sum of a and b is 4




https://wap.sciencenet.cn/blog-3031432-1058092.html

上一篇:Python3:字符串 str(), repr(), eval()
下一篇:Python3: 格式化输出 print()
收藏 IP: 134.1.1.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-5-23 21:18

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部