如何使用Python中的输入输入整数

The*_*but 5 python int input python-3.x

我正在尝试自学如何使用Python进行编码,这是我第一次在Stack Overflow上发帖,因此,请原谅本文中的任何不当之处。但是,让我们开始吧。

我正在尝试使用输入命令返回一个整数。我也已经完成了研究,因此以下是我在Python 3.4中的多次尝试以及随之而来的结果:

尝试#1

guess_row = int(input("Guess Row: "))
Run Code Online (Sandbox Code Playgroud)

我得到以下信息:

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

尝试#2

guess_row = float(input("Guess Row: "))
Run Code Online (Sandbox Code Playgroud)

我得到以下信息:

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

尝试#3

try:
    guess_row=int(input("Guess Row: "))
except ValueError:
    print("Not an integer")
Run Code Online (Sandbox Code Playgroud)

在这里,我得到以下信息:

Guess Row: 2
Not an integer
Run Code Online (Sandbox Code Playgroud)

尽管它返回了某些内容,但我知道这是错误的,因为,其中之一是,输入以字符串形式返回,并且还返回了print命令。

要点是,我已经尝试过int,float和try,但到目前为止没有任何效果。有什么建议么?我只希望能够输入一个整数并将其返回为一个整数。