擅长:python、mysql、java
<p><code>for</code>循环只是在对象集合上迭代。考虑一个列表<code>my_list = ['a', 'b', 'c']</code>。在<code>my_list</code>上使用<code>for</code>循环的每次迭代中,它都按顺序获取一个元素,而不重复。<code>range(2)</code>等价于<code>[0, 1]</code>。你知道吗</p>
<p>试试这个:</p>
<pre><code>print("Available subjects:\n-Algebra\n-Computing\n")
for i in range(2):
# `i` is 0 on first iteration and 1 on second. We are not using `i` anywhere since all we want is to loop :)
chosen_subject = input("What subject would you like to do? ")
if chosen_subject.lower() in subjects:
print("\n")
break
if chosen_subject.lower() in subjects:
print("working")
else:
print("You keep typing in something incorrect.\nPlease restart the program.")
</code></pre>