使用Python在一行中读取两个变量 [英] Read two variables in a single line with Python

查看:360
本文介绍了使用Python在一行中读取两个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对input()函数很熟悉,它可以从用户输入中读取单个变量.有没有类似的简单方法可以读取两个变量?

I am familiar with the input() function, to read a single variable from user input. Is there a similar easy way to read two variables?

我正在寻找与之等效的东西

I'm looking for the equivalent of:

scanf("%d%d", &i, &j); // accepts "10 20\n"

我能够实现这一目标的一种方法是使用raw_input(),然后使用split输入的内容.有没有更优雅的方式?

One way I am able to achieve this is to use raw_input() and then split what was entered. Is there a more elegant way?

这不是供现场使用.只是为了学习.

This is not for live use. Just for learning..

推荐答案

不,通常的方法是raw_input().split()

在您的情况下,如果希望它们是整数而不是字符串,则可以使用map(int, raw_input().split())

In your case you might use map(int, raw_input().split()) if you want them to be integers rather than strings

请勿使用input().考虑如果用户输入

Don't use input() for that. Consider what happens if the user enters

import os;os.system('do something bad')

这篇关于使用Python在一行中读取两个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆