我在 python 中尝试的这个循环有什么问题?
Whats the problem with this loop im trying in python?
完全是新手。我正在尝试读取用户的输入并打印我使用循环设置的相应含义,但我发现它有问题,因为它没有按我的预期工作。它适用于所有 3 个输入作为 1,但如果不这样做,情况看起来不太好。
options = [
["1. opt1", "2. opt2", "3. opt3"],
["1. opt1", "2. opt2", "3. opt3"],
["1. opt1", "2. opt2", "3. opt3", "4. opt4"]
]
meaning = [
[
["football"],
["basketball"],
["baseball"],
["Invalid"]
],
[
["water"],
["cocacola"],
["milk"],
["Invalid"]
],
[["human"],
["dogs"],
["cats"],
["robots"]
]
]
number = [1, 2, 3]
#question 1 and their options displayed here
number[0] = int(input(
"Type the number you see in front of the option you see above: "))
#question 2 and their options displayed here
number[0] = int(input(
"Type the number you see in front of the option you see above: "))
#question 3 and their options displayed herenumber[0] = int(input(
number[0] = int(input(
"Type the number you see in front of the option you see above: "))
i = 0
j = 0
for i in range(len(options)):
for j in range(len(meaning[i])):
if number[i] == i+1:
print(meaning[i][j])
break
else:
j += 1
i += 1
如果您想回答第二个和第三个意思,请考虑用户
number[1] = int(input(...))
number[2] = int(input(...))
而不是
number[0] = int(input(...))
使用 Konstantin Ushakov 的回答。
和:
您在 for-loops 中失败:
试试看:
if number[i] == j+1:
完全是新手。我正在尝试读取用户的输入并打印我使用循环设置的相应含义,但我发现它有问题,因为它没有按我的预期工作。它适用于所有 3 个输入作为 1,但如果不这样做,情况看起来不太好。
options = [
["1. opt1", "2. opt2", "3. opt3"],
["1. opt1", "2. opt2", "3. opt3"],
["1. opt1", "2. opt2", "3. opt3", "4. opt4"]
]
meaning = [
[
["football"],
["basketball"],
["baseball"],
["Invalid"]
],
[
["water"],
["cocacola"],
["milk"],
["Invalid"]
],
[["human"],
["dogs"],
["cats"],
["robots"]
]
]
number = [1, 2, 3]
#question 1 and their options displayed here
number[0] = int(input(
"Type the number you see in front of the option you see above: "))
#question 2 and their options displayed here
number[0] = int(input(
"Type the number you see in front of the option you see above: "))
#question 3 and their options displayed herenumber[0] = int(input(
number[0] = int(input(
"Type the number you see in front of the option you see above: "))
i = 0
j = 0
for i in range(len(options)):
for j in range(len(meaning[i])):
if number[i] == i+1:
print(meaning[i][j])
break
else:
j += 1
i += 1
如果您想回答第二个和第三个意思,请考虑用户
number[1] = int(input(...))
number[2] = int(input(...))
而不是
number[0] = int(input(...))
使用 Konstantin Ushakov 的回答。 和: 您在 for-loops 中失败: 试试看:
if number[i] == j+1: