在python中扫描用户输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15753376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:54:05  来源:igfitidea点击:

scanning user input in python

python

提问by user2232076

direction = input("enter a direction: ")
if direction != "quit" and direction != "go north" and direction != "go south" and direction != "go east" and direction != "go west" and direction != "go up" and direction != "go down" and direction != "look":
    print ("please enter in the following format, go (north,east,south,west,up,down)")

elif direction == "quit":
    print ("OK ... but a small part of you may never leave until you have personally saved Muirfieland from the clutches of evil .. Bwahahahahahah (sinister laugh) ... the game should then end.")

elif direction == "look":
    print ("You see nothing but endless void stretching off in all directions ...")

else:
    print ("You wander of in the direction of " + direction)

i need to know how to do this in python. i need to scan user inputs first 2 letters for example

我需要知道如何在 python 中做到这一点。例如,我需要扫描用户输入的前 2 个字母

i = user_input
#user inputs go ayisgfdygasdf

i need it to be able to scan the user input, check if the first 2 letters are go, and if they are go but it doesnt recognise the second word which in this case is "ayisgfdygasdf" then to print "sorry, i cant do that"

我需要它能够扫描用户输入,检查前两个字母是否消失,如果它们消失,但它无法识别第二个单词,在这种情况下是“ayisgfdygasdf”然后打印“对不起,我不能做那”

回答by e.beyer

You can access characters of a string in python by index using the [] notation. You can check the first two character in a string by typing user_input[:2]. This code will include all characters up to, but not including the index typed. So this notation will include user_input[0] and user_input[1]. You can then check if user_input[:2] is equal to 'go' or not, and continue from there.

您可以使用 [] 表示法通过索引访问 python 中字符串的字符。您可以通过键入 user_input[:2] 来检查字符串中的前两个字符。此代码将包括所有字符,但不包括键入的索引。所以这个符号将包括 user_input[0] 和 user_input[1]。然后,您可以检查 user_input[:2] 是否等于 'go',然后从那里继续。

Hope this helped.

希望这有帮助。

回答by Juan Carlos Moreno

Instead try using:

而是尝试使用:

direction = sys.stdin.readlines()

It may require you to ctrl+D after you are done but you will be able to capture so much more.

完成后可能需要您按 ctrl+D,但您将能够捕获更多内容。

Also, to get the subarray you can then you can even check:

此外,要获得子数组,您甚至可以检查:

direction[:2] != "go"

or alternatively, for more readable code:

或者,对于更具可读性的代码:

if not direction.startswith("go"):

Also I'd recommend, for making your code more readable,

我还建议,为了使您的代码更具可读性,

defined_direction = frozenset(["quit", "go north", "go south"])
if( direction not in defined_direction):
   print "please enter...."

回答by Disconnect3d

He could also try using:

他也可以尝试使用:

    directions.split()

But it may require to use try/except in some cases.

但在某些情况下可能需要使用 try/except 。

For more information about split and methods try using:

有关拆分和方法的更多信息,请尝试使用:

    dir(directions)

to see what methods object directions have

查看对象方向有哪些方法

or:

或者:

    help(directions.split) 

to see help about a specific method (in this case method split of object directions)

查看有关特定方法的帮助(在本例中为对象方向的方法拆分)

回答by dckrooney

You can index the individual characters of your input:

您可以索引输入的单个字符:

if direction[:2] == "go":
    print "Sorry, I can't do that."

However, trying assign an if-else branch to each possible input is typically a bad choice... It becomes difficult to maintain very quickly.

然而,尝试为每个可能的输入分配一个 if-else 分支通常是一个糟糕的选择......它变得很难快速维护。

A cleaner approach in this case might be to define a dictionary with valid input as follows:

在这种情况下,一种更简洁的方法可能是定义一个具有有效输入的字典,如下所示:

input_response = {"quit":"OK ... but", "go north": "You wander off north", \
                  "go south": "You wander off south"} # etc

You could then re-write your code to something like:

然后,您可以将代码重新编写为:

try:
    print input_response[direction]
except KeyError:
    if direction[:2] == "go":
        print "Sorry, I can't do that."
    else:
        print ("please enter in the following format...")