python中的怎么打出来_如何在python中输入输入

1586010002-jmsa.png

I am taking part in a competition, but have no idea how to make python take the input. Here's a typical example . The site has given example of how to take input but those are using C and Java ( click here ). Please help me figure out how to make python take the input in this case.

解决方案

There's more than one way to do this.

Taking input from the intepreter

For Python 2.x, you can use the raw_input() function:

my_input = raw_input("Please enter an input: ")

#do something with my_input

Note that the input is always a string. To retrieve a number, you can use the built-in int() function:

my_input = int(raw_input("Please enter an input: "))

#do something with my_input

As one other answer mentioned, this will throw an error if the input is a float.

There's also another function, input, in Python 2.x. However, in this version of Python, input evaluates the input, which is a bad idea. It's not recommended to use it.

For Python 3.x, however, you can use the input() function without any problem, since it's a replacement for raw_input:

my_input = input("Please enter an input: ")

#do something with my_input

Taking input from command line arguments

You can also retrieve your input from command line arguments, when executing your script like this:

$ python my_script.py arg1 arg2

The arguments will be stored in the list sys.argv. sys.argv[0] is the first argument, sys.argv[1] is the second argument, and so on.

Example:

import sys

my_input = sys.argv[0]

#do something with my_input

See the details of it here

This method works for both versions, Python 3.x and 2.x .

Hope this helps!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值