Python3中的输入输出
input()函数
我们可以通过Python3解释器查看Python3中input()的含义:
>>> type(input)
<class 'builtin_function_or_method'>
>>> help(input) Help on built-in function input in module builtins: input(prompt=None, /)
Read a string from standard input. The trailing newline is stripped. The prompt string, if given, is printed to standard output without a
trailing newline before reading input. If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
On *nix systems, readline is used if available.
(END)
即:打印提示字符串(如果给定)到标准输出,并从标准输入中读取字符串,尾部换行符被剥离。如果用户输入EOF,会触发EOFError。
请注意,Python3中input()一次读取一行,并当作字符串,与Python2中的raw_input()相同。例如:
>>> a = input()
a b c d
>>> a
'a b c d'
>>> a = input()
3.1425926
>>> a
'3.1425926'
三种示例
在OJ上做题比较常见。
'''
Python的输入是野生字符串,所以要自己转类型
strip去掉首尾的空格和换行符(默认时),返回str
slipt把字符串按空格拆开(默认时),返回[str]
map把list里面的值映射到指定类型,返回[type]''' #多组测试数据,没告诉具体组数,处理到文件结束。
while True:
try:
a, b = map(int, input().strip().split())
print(a + b)
except EOFError:
break #输入一个整数告诉你接下来有多少组,接着输入每组数据
T = int(input().strip())
for case in range(T):
a, b = map(int, input().strip().split())
print(a + b) #多组测试数据,没告诉具体组数,但满足一定条件结束
while True:
a, b = map(int, input().strip().split())
if a == 0 and b == 0:
break
print(a + b)
有兴趣可以去这里提交一下:https://vjudge.net/problem/HRBUST-1883
参考链接:
1、https://blog.csdn.net/luovilonia/article/details/40860323
2、https://blog.csdn.net/lt17307402811/article/details/77184179
3、https://blog.csdn.net/desporado/article/details/81409573
Python3中的输入输出的更多相关文章
- Python3中的字符串函数学习总结
这篇文章主要介绍了Python3中的字符串函数学习总结,本文讲解了格式化类方法.查找 & 替换类方法.拆分 & 组合类方法等内容,需要的朋友可以参考下. Sequence Types ...
- Python3中使用PyMySQL连接Mysql
Python3中使用PyMySQL连接Mysql 在Python2中连接Mysql数据库用的是MySQLdb,在Python3中连接Mysql数据库用的是PyMySQL,因为MySQLdb不支持Pyt ...
- python3 中mlpy模块安装 出现 failed with error code 1的决绝办法(其他模块也可用本方法)
在python3 中安装其它模块时经常出现 failed with error code 1等状况,使的安装无法进行.而解决这个问题又非常麻烦. 接下来以mlpy为例,介绍一种解决此类安装问题的办法. ...
- python3中返回字典的键
我在看<父与子的编程之旅>的时候,有段代码是随机画100个矩形,矩形的大小,线条的粗细,颜色都是随机的,代码如下, import pygame,sys,random from pygame ...
- python3中的zip
在 python2 中zip可以将两个列表并入一个元组列表,如: a = [1,2,3,4] b = [5,6,7,8] c = zip(a,b) 结果:c [(1,5),(2,6),(3,7),(4 ...
- python3中输出不换行
python2中输出默认是换行的,为了抑制换行,是这么做的: print x, 到了python3中,print变成一个函数,这种语法便行不通了.用2to3工具转换了下,变成这样了: print(x, ...
- Python3中的新特性(3)——代码迁移与2to3
1.将代码移植到Python2.6 建议任何要将代码移植到Python3的用户首先将代码移植到Python2.6.Python2.6不仅与Python2.5向后兼容,而且支持Python3中的部分新特 ...
- Python3中的新特性(1)——新的语言特性
1.源代码编码和标识符 Python3假定源代码使用UTF-8编码.另外,关于标识符中哪些字符是合法的规则也放宽了.特别是,标识符可以包含代码点为U+0080及以上的任意有效Unico ...
- python3中socket套接字的编码问题解决
一.TCP 1.tcp服务器创建 #创建服务器 from socket import * from time import ctime #导入ctime HOST = '' #任意主机 PORT = ...
随机推荐
- bzoj2004公交线路——DP+矩阵加速递推
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2004 求方案数,想到DP: 因为两个站间距离<=p,所以每p个站中所有车一定都会停靠至 ...
- 洛谷 - P2293 - 高精度开根 - 高精度
https://www.luogu.org/problemnew/show/P2293 要求求出给定高精度整数的非负根取整的结果. 还有神仙用Python的浮点pow运算骗到不少分的. 唉! 那么我们 ...
- 洛谷 - P1115 - 最大子段和 - 简单dp
https://www.luogu.org/problemnew/show/P1115 简单到不想说……dp[i]表示以i为结尾的最大连续和的值. 那么答案肯定就是最大值了.求一次max就可以了. 仔 ...
- 878. Nth Magical Number
A positive integer is magical if it is divisible by either A or B. Return the N-th magical number. ...
- hdoj3665【简单DFS】
题意: 略. 思路: n就10而已,没有环,搜一下就好了.. #include <bits/stdc++.h> using namespace std; typedef long long ...
- Ubuntu添加新用户,并且赋sudo权限
https://blog.csdn.net/u012897374/article/details/78827359 sudo adduser username 接下来进入root用户,如果之前就没有普 ...
- android ViewPager 与Fragment
ViewPager 左右滑动数据显示 1. 整体布局 FragmentLayout 容器包裹Fragment <?xml version="1.0" encoding=&qu ...
- 莫比乌斯函数 && HDU-1695
莫比乌斯函数定义: $$\mu(d)=\begin{cases}1 &\text{d = 1}\\(-1)^r &\text{$d=p_1p_2...p_r,其中p_i为不同的素数$} ...
- Mike and gcd problem CodeForces - 798C
题目 (智商题 or 糟心的贪心) 题意: 有一个数列a1,a2,...,an,每次操作可以将相邻的两个数x,y变为x-y,x+y,求最少的操作数使得gcd(a1,a2,...,an)>1.gc ...
- Win10 Hyper-v 中安装 CentOS 搭建开发环境
Windows 环境 操作系统:Windows 10 开发环境:VS2005(需启动.NET Framework 3.5 ,才能正常安装使用) Linux 环境 发行版:CentOS 7_x64 安 ...