Python输入循环取决于输入的数字

Python输入循环取决于输入的数字,python,Python,如何让python基于输入响应提出相同的问题。假设我问以下问题,您今天想要配置多少组?用户以10作为响应。我希望python允许用户输入10个不同的组名,以便python要求它输入10个输入。根据输入,我会把车拿走 您可以使用for循环(或者,如果您愿意,可以使用列表): 如果使用Python3.x,请将raw_input更改为input n = int(raw_input('How many groups would you like to configure today? ')) for i

如何让python基于输入响应提出相同的问题。假设我问以下问题,您今天想要配置多少组?用户以10作为响应。我希望python允许用户输入10个不同的组名,以便python要求它输入10个输入。根据输入,我会把车拿走

您可以使用for循环(或者,如果您愿意,可以使用列表):


如果使用Python3.x,请将
raw_input
更改为
input

n = int(raw_input('How many groups would you like to configure today? '))
for i in range(n):
    group = raw_input('Group {}: '.format(i+1))
    # Do something with group...
差不多

resp = raw_input('How many groups would you like to configure today? ')
try:
    num = int(resp)
except ValueError as e:
    # Handle error

groups = []
for i in range(num):
    resp = raw_input('Enter group name: ')
    groups.append(resp)

# The rest (at this point, the group names will be in the list "groups")

…应该有效。主要部分是
raw_input
和使用
append
将响应推送到列表中。此外,请确保您能够处理这样的情况,即用户输入类似“2”的内容,或者只按enter键而不是数字(使用
try
/
除了
)。

我试图让它在用户输入时打印出漂亮和难看的内容,但它只记住最后的输入n=int(输入)范围(n)中的i的(‘您今天要配置多少组?’):组=输入(‘组{}:’。格式(i+1))定义uqdfa(货币):返回(‘允许’+货币)(如果组=‘尼斯’):打印(uqdfa(‘233.10.10.10’)打印(uqdfa(‘233.10.10.11’)如果组=‘丑陋’:打印(uqdfa(‘233.10.10.16’)打印(uqdfa(‘233.10.10.17’)@JoelMercado,在您的评论中很难阅读代码。请对代码提出疑问。
#Handle error
需要由处理异常的代码替换。现在,您可以将其替换为
pass
,但这与错误处理的目的背道而驰。
resp = raw_input('How many groups would you like to configure today? ')
try:
    num = int(resp)
except ValueError as e:
    # Handle error

groups = []
for i in range(num):
    resp = raw_input('Enter group name: ')
    groups.append(resp)

# The rest (at this point, the group names will be in the list "groups")