如何使用Python中的输入输入整数 [英] How do you input integers using input in Python

查看:1676
本文介绍了如何使用Python中的输入输入整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自学如何使用Python进行编码,这是我第一次发布到Stack Overflow,所以请原谅这篇文章中的任何不当行为。但是让我们做对了。

I'm trying to teach myself how to code in Python and this is my first time posting to Stack Overflow, so please excuse any improprieties in this post. But let's get right to it.

我正在尝试使用input命令返回一个整数。我也完成了我的研究,所以下面是我在Python 3.4中的多次尝试以及随后的结果:

I'm trying to use the input command to return an integer. I've done my research, too, so below are my multiple attempts in Python 3.4 and the results that follow:

guess_row = int(input("Guess Row: "))

我收到以下信息:

Traceback (most recent call last):
File "<input>", line 1, in <module> 
ValueError: invalid literal for int() with base 10: 'Guess Row: 2`



尝试#2



Attempt #2

guess_row = float(input("Guess Row: "))

我收回以下内容:

Traceback (most recent call last):
File "<input>", line 1, in <module>
ValueError: could not convert string to float: "Guess Row: 2""



尝试#3



Attempt #3

try:
    guess_row=int(input("Guess Row: "))
except ValueError:
    print("Not an integer")

在这里,我回过头来看:

Here, I get back the following:

Guess Row: 2
Not an integer

虽然它返回了一些内容,但我知道这是错误的,因为,对于一个,输入以字符串形式返回,它也返回打印命令。

Although it returns something, I know this is wrong because, for one, the input returns as a string and it also returns the print command.

指出,我已经尝试过int,float和try,到目前为止没有任何工作。有什么建议吗?我只想输入一个整数并让它作为一个返回。

Point being, I've tried int, float, and try, and so far nothing has worked. Any suggestions? I just want to be able to input an integer and have it returned as one.

推荐答案


然而,我注意到一些奇怪的事情。如果我只是通过使用运行整个代码的传统运行(即绿色按钮),而不是尝试执行个别行按F2键的代码。有谁知道为什么会出现这种情况?

However, I noticed something odd. The code works if I run it just by using the traditional run (i.e., the green button) that runs the entire code, rather than trying to execute individuals lines of code by pressing F2. Does anyone know why this may be the case?

这似乎是Eclipse中的问题,来自 PyDev常见问题解答

This seems to be a problem in Eclipse, from the PyDev FAQ:


为什么raw_input()/ input()在PyDev中无法正常工作?

eclipse控制台不是shell的精确副本...其中一个更改是当你按下shell时,它可能会给你一个\r,\ n或\\\\ n作为一个终点字符串,取决于您的平台。 Python并不期望这样 - 从文档来看,它会删除最后的\ n(在版本2.4中检查),但是,在某些平台上会留下\r。这意味着raw_input()通常应该用作raw_input()。replace('\ r','')和input()应该更改为:eval(raw_input()。replace('\ r') ,''))。

The eclipse console is not an exact copy of a shell... one of the changes is that when you press in a shell, it may give you a \r, \n or \r\n as an end-line char, depending on your platform. Python does not expect this -- from the docs it says that it will remove the last \n (checked in version 2.4), but, in some platforms that will leave a \r there. This means that the raw_input() should usually be used as raw_input().replace('\r', ''), and input() should be changed for: eval(raw_input().replace('\r', '')).

另见:
Eclipse 4中的PyDev 3.7.1 - input()将提示字符串添加到输入变量?
无法在Eclipse上的PyDev控制台中使用Jython提供用户输入

这篇关于如何使用Python中的输入输入整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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