如何指定输入?

发布于 2025-02-10 03:52:01 字数 257 浏览 2 评论 0原文

输入吗

input("Enter a word and a number: ")

Enter a word and a number: hello 14

有人知道如何执行这样的

有人知道这样做的“作弊”吗?

Does someone know how to do an input like that:

input("Enter a word and a number: ")

and in the console we write:

Enter a word and a number: hello 14

It reconize it like [word] [number].

Does someone know a "cheat" to do something like that ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南巷近海 2025-02-17 03:52:01

input()是一个超简单的功能,它将用户输入将用户输入作为字符串吐出到变量中。如果您知道输入的格式,则可以使用正则表达式(如果您熟悉C的scanf(),则可以觉得这很有用:

在这种情况下,因此:

import re

user_in = input('Enter a word then a number: ')
# Example: bird 15

match = re.search('(\S+) ([-+]?\d+)', user_in)
if match:
    word, number = match.group(1, 2)

print(word)
# Output: bird

print(number)
# Output: 15

print(number + 10)
# Output: 25

请参阅: https:// https:// https:// docs.python.org/3/library/re.html#match-objects

input() is a super simple function that will just spit out the user input as a string into a variable. If you know the format the input should be in though, you can parse it using regular expressions (if you're familiar with C's scanf() you may find this useful: https://docs.python.org/3/library/re.html#simulating-scanf)

In this case, as such:

import re

user_in = input('Enter a word then a number: ')
# Example: bird 15

match = re.search('(\S+) ([-+]?\d+)', user_in)
if match:
    word, number = match.group(1, 2)

print(word)
# Output: bird

print(number)
# Output: 15

print(number + 10)
# Output: 25

See: https://docs.python.org/3/library/re.html#match-objects

妞丶爷亲个 2025-02-17 03:52:01
inp = input("Enter a word and a number: ").split()

inp [0]将是单词和int(inp [1])将是数字。

重要的是要注意,带有空参数的.split()将自动解释为.split('')

inp = input("Enter a word and a number: ").split()

inp[0] will be the word and int(inp[1]) will be the number.

It is important to note that .split() with an empty argument will automatically be interpreted as .split(' ')

仄言 2025-02-17 03:52:01
w, n = input("Enter a word and a number:").split(' ')
n = int(n) # to cast the value of the number
w, n = input("Enter a word and a number:").split(' ')
n = int(n) # to cast the value of the number
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文