如何读取键盘输入? [英] How to read keyboard-input?

查看:35
本文介绍了如何读取键盘输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在python中从键盘读取数据

I would like to read data from the keyboard in python

我试试这个:

nb = input('Choose a number')
print ('Number%s 
' % (nb))

但它不起作用,无论是在 eclipse 还是在终端中,问题总是停止.我可以输入一个数字,但什么也没发生.

But it doesn't work, neither with eclipse nor in the terminal, it's always stop of the question. I can type a number but after nothing happen.

你知道为什么吗?

推荐答案

使用

input('Enter your input:')

如果您使用 Python 3.

if you use Python 3.

如果你想要一个数值,只需转换它:

And if you want to have a numeric value, just convert it:

try:
    mode = int(input('Input:'))
except ValueError:
    print("Not a number")

如果您使用 Python 2,则需要使用 raw_input 而不是 input.

If you use Python 2, you need to use raw_input instead of input.

这篇关于如何读取键盘输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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