擅长:python、mysql、java
<p>不要使用<code>str</code>作为变量名,它会隐藏一个重要的内建项并导致奇怪的问题。你知道吗</p>
<p>使用while循环将输出限制为有效选项。你知道吗</p>
<pre><code>valid_choices = ('d', 'l', 'r', 'u', 'b',)
choice = None
while choice not in valid_choices:
text = input("Which direction do you steer your head? d= down, l= left, r= right, u= up, b= behind you: ")
choice = text.strip()
if choice == 'd':
print ('...')
elif choice == 'u':
print ('...')
</code></pre>
<p>另请参见:</p>
<ul>
<li><a href="https://docs.python.org/2/library/string.html#string.strip" rel="nofollow">^{<cd2>}</a></li>
<li><a href="https://docs.python.org/2/tutorial/datastructures.html#tuples-and-sequences" rel="nofollow">tuples</a></li>
<li><a href="https://docs.python.org/2/library/constants.html#None" rel="nofollow">^{<cd3>}</a></li>
</ul>