我正在努力用Python结束我的程序,我只想输入q
或quit
来完成程序。这是我的代码
# This is a guess the number game
import random
guessesTaken = 0
print('Hello! What is your name?')
myName = input()
number = random.randint(1, 20)
print('Well, ' + myName + ', I am thinking of a number between 1 and 20, can you guess it?')
while guessesTaken < 6:
print('Take a guess!')
guess = input()
guess = int(guess)
guessesTaken = guessesTaken + 1
if guess < number:
print('Your guess is too low')
if guess > number:
print('Your guess is too high')
if guess == number:
break
if guess == number:
guessesTaken = str(guessesTaken)
print('Good job ' + myName + '!You guessed my number in ' + guessesTaken + ' guesses!')
if guess != number:
number = str(number)
print('Nope. The number I was thinking of was ' + number)
print('Game Over')
choice = input('Press Q to Quit')
if choice == 'q' :
sys.exit()
请告诉我哪里出错了?
答案 0 :(得分:2)
据我所知,你有两个错误。
首先,您需要导入sys
sys.exit()
模块才能正常工作。
其次,你的缩进在两个地方是不正确的。这是正确的代码。评论显示缩进不正确的地方:
# This is a guess the number game
import random
import sys
guessesTaken = 0
print('Hello! What is your name?')
myName = input()
number = random.randint(1, 20)
print('Well, ' + myName + ', I am thinking of a number between 1 and 20, can you guess it?')
while guessesTaken < 6:
print('Take a guess!')
guess = input()
guess = int(guess)
guessesTaken = guessesTaken + 1
if guess < number:
print('Your guess is too low')
if guess > number:
print('Your guess is too high')
if guess == number:
break
if guess == number:
guessesTaken = str(guessesTaken)
print('Good job ' + myName + '!You guessed my number in ' + guessesTaken + ' guesses!')
if guess != number: #indentation was off here
number = str(number)
print('Nope. The number I was thinking of was ' + number)
print('Game Over') #and here
choice = input('Press Q to Quit')
if choice == 'q' :
sys.exit() # and here
答案 1 :(得分:1)
正如@MarkyPython所说,你有一些缩进错误,如果你想使用exit,你需要导入sys。
请注意,鉴于您拥有的代码,使用 <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<div class="container">
<table class="responsive-table">
<caption>Top 10 Grossing Animated Films of All Time</caption>
<thead>
<tr>
<th scope="col">Film Title</th>
<th scope="col">Released</th>
<th scope="col">Studio</th>
<th scope="col">Worldwide Gross</th>
<th scope="col">Domestic Gross</th>
<th scope="col">Foreign Gross</th>
<th scope="col">Budget</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="7">Sources: <a href="http://en.wikipedia.org/wiki/List_of_highest-grossing_animated_films" rel="external">Wikipedia</a> & <a href="http://www.boxofficemojo.com/genres/chart/?id=animation.htm" rel="external">Box Office Mojo</a>. Data is current as of September 1, 2015.</td>
</tr>
</tfoot>
<tbody>
<tr>
<th scope="row">Frozen</th>
<td data-title="Released">2013</td>
<td data-title="Studio">Disney</td>
<td data-title="Worldwide Gross" data-type="currency">$1,279,852,693</td>
<td data-title="Domestic Gross" data-type="currency">$400,738,009 </td>
<td data-title="Foreign Gross" data-type="currency">$873,481,000</td>
<td data-title="Budget" data-type="currency"><div id="datepicker1">
</div></td>
</tr>
<tr>
<th scope="row">Toy Story 3</th>
<td data-title="Released">2010</td>
<td data-title="Studio">Disney Pixar</td>
<td data-title="Worldwide Gross" data-type="currency">$1,063,171,911</td>
<td data-title="Domestic Gross" data-type="currency">$415,004,880</td>
<td data-title="Foreign Gross" data-type="currency">$648,167,031</td>
<td data-title="Budget" data-type="currency">$200,000,000</td>
</tr>
<tr>
<th scope="row">Minions</th>
<td data-title="Released">2015</td>
<td data-title="Studio">Universal</td>
<td data-title="Worldwide Gross" data-type="currency">$1,018,901,000</td>
<td data-title="Domestic Gross" data-type="currency">$324,755,670</td>
<td data-title="Foreign Gross" data-type="currency">$694,100,000 </td>
<td data-title="Budget" data-type="currency">$74,000,000</td>
</tr>
<tr>
<th scope="row">The Lion King</th>
<td data-title="Released">1994</td>
<td data-title="Studio">Disney</td>
<td data-title="Worldwide Gross" data-type="currency">$987,483,777</td>
<td data-title="Domestic Gross" data-type="currency">$422,783,777</td>
<td data-title="Foreign Gross" data-type="currency">$564,700,000</td>
<td data-title="Budget" data-type="currency">$45,000,000</td>
</tr>
<tr>
<th scope="row">Despicable Me 2</th>
<td data-title="Released">2013</td>
<td data-title="Studio">Universal</td>
<td data-title="Worldwide Gross" data-type="currency">$970,761,885</td>
<td data-title="Domestic Gross" data-type="currency">$368,061,265</td>
<td data-title="Foreign Gross" data-type="currency">$602,700,620</td>
<td data-title="Budget" data-type="currency">$76,000,000</td>
</tr>
<tr>
<th scope="row">Finding Nemo</th>
<td data-title="Released">2003</td>
<td data-title="Studio">Pixar</td>
<td data-title="Worldwide Gross" data-type="currency">$936,743,261</td>
<td data-title="Domestic Gross" data-type="currency">$380,843,261</td>
<td data-title="Foreign Gross" data-type="currency">$555,900,000</td>
<td data-title="Budget" data-type="currency">$94,000,000</td>
</tr>
<tr>
<th scope="row">Shrek 2</th>
<td data-title="Released">2004</td>
<td data-title="Studio">Dreamworks</td>
<td data-title="Worldwide Gross" data-type="currency">$919,838,758</td>
<td data-title="Domestic Gross" data-type="currency">$441,226,247</td>
<td data-title="Foreign Gross" data-type="currency">$478,612,511</td>
<td data-title="Budget" data-type="currency">$150,000,000</td>
</tr>
<tr>
<th scope="row">Ice Age: Dawn of the Dinosaurs</th>
<td data-title="Released">2009</td>
<td data-title="Studio">Fox</td>
<td data-title="Worldwide Gross" data-type="currency">$886,686,817</td>
<td data-title="Domestic Gross" data-type="currency">$196,573,705</td>
<td data-title="Foreign Gross" data-type="currency">$690,113,112 </td>
<td data-title="Budget" data-type="currency">$90,000,000</td>
</tr>
<tr>
<th scope="row">Ice Age: Continental Drift</th>
<td data-title="Released">2012</td>
<td data-title="Studio">Fox</td>
<td data-title="Worldwide Gross" data-type="currency">$877,244,782</td>
<td data-title="Domestic Gross" data-type="currency">$161,321,843</td>
<td data-title="Foreign Gross" data-type="currency">$715,922,939</td>
<td data-title="Budget" data-type="currency">$95,000,000</td>
</tr>
<tr>
<th scope="row">Shrek the Third</th>
<td data-title="Released">2007</td>
<td data-title="Studio">Dreamworks</td>
<td data-title="Worldwide Gross" data-type="currency">$798,958,162</td>
<td data-title="Domestic Gross" data-type="currency">$322,719,944</td>
<td data-title="Foreign Gross" data-type="currency">$476,238,218</td>
<td data-title="Budget" data-type="currency">$160,000,000</td>
</tr>
</tbody>
</table>
</div>
实际上是不好的做法。我建议您使用sys.exit()
。这将退出你的while循环。因为之后你没有代码,你的程序将会慷慨退出。
如果稍后添加更多代码,您可能希望将代码放在函数中。此时,使用break
将是最佳选择。
使用return
或break
是更好的做法,因为它可以让以后更轻松地为您的程序添加功能,并使您的代码流更好(不会突然退出'跳')。
return