如何通过input()输入多个字符串 [英] How to enter multiple strings via input()
本文介绍了如何通过input()输入多个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在python中使用raw_input时,用户必须输入一个输入然后按回车.有没有办法让我编码一些东西,代码会同时提示多个用户输入,然后按回车键运行代码?
例如:而不是...
<预><代码>>>>名称:<提示><输入>>>>年龄:<提示><输入>>>>性别:<提示><输入>它会有……
<预><代码>>>>名称:<提示>年龄:<提示>性别:<提示><输入> 解决方案
否,input()
只允许提供一个字符串输入.
你能做的是:
name,age,gender = input('输入姓名|年龄|性别:').split('|')# 用户输入ABC|30|M"打印(姓名、年龄、性别)# ABC 30 M
现在,您只需要依靠名称中没有 |
字符的用户即可.
当然,您也可以提出不同的问题.
When using raw_input in python, a user has to put an input and then press enter. Is there a way for me to code something, where the code will prompt several user inputs at the same time and THEN press enter for the code to run?
For example: instead of ...
>>> Name: <prompt> <enter>
>>> Age: <prompt> <enter>
>>> Gender: <prompt> <enter>
it'll have ...
>>>
Name: <prompt>
Age: <prompt>
Gender: <prompt>
<enter>
解决方案
No, input()
only allows one string input to be supplied.
What you can do is this:
name, age, gender = input('Enter Name|Age|Gender:').split('|')
# user inputs "ABC|30|M"
print(name, age, gender)
# ABC 30 M
Now you just rely on user not having a |
character in their name.
Or, of course, you can ask separate questions.
这篇关于如何通过input()输入多个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文