我的python代碼如下,但它不起作用。爲什麼我的python代碼不能正常運行?
import random
secret = random.randint (1, 99)
guess = 0
tries = 0
print "AHOY! I'm the Dread Pirate Oliver and I have a secret!"
print "I will tell you where my treasure is buried if you can guess the number that I'm thinking of."
print "It is a number from 1 to 99. I'll give you 6 tries."
while guess != secret and tries < 6:
guess = input ("What's yer guess? ")
if guess < secret:
print "Too low ye scurvy dog!"
elif guess > secret:
print "Too high, landlubber!"
tries = tries + 1
if guess == secret:
print "Avast! Ye got it! Found ma secret number, ye did!"
print "THE FIRST WORD IS: Awesome"
else:
print "No more guesses! Better luck next time, Matey!"
print "Ma secret number wuz", secret
raise SystemExit()
import random
secret = random.randint (1, 99)
guess = 0
tries = 0
print "AHOY THERE!"
print "ME AGAIN"
print "I will tell you the second word if you can guess the number that I'm thinking of."
print "It is a number from 1 to 99. I'll give you 6 tries."
while guess != secret and tries < 6:
guess = input ("What's yer guess? ")
if guess < secret:
print "Too low ye scurvy dog!"
elif guess > secret:
print "Too high, landlubber!"
tries = tries + 1
if guess == secret:
print "Avast! Ye got it! Found ma secret number, ye did!"
print "THE SECOND WORD IS: Land"
else:
print "No more guesses! Better luck next time, Matey!"
print "Ma secret number wuz", secret
raise SystemExit()
import random
secret = random.randint (1, 3)
guess = 0
tries = 0
print "AHOY! One more thing"
print "It is a number from 1 to 3. I'll give you 1 try."
while guess != secret and tries < 1:
guess = input ("What's yer guess? ")
if guess < secret:
print "Too low ye scurvy dog!"
elif guess > secret:
print "Too high, landlubber!"
tries = tries + 1
if guess == secret:
print "Avast! Ye got it! Found ma secret number, ye did!"
print "It's buried in the sand at 36 degrees North, 48 degrees east."
else:
print "No more guesses! Better luck next time, Matey!"
print "Ma secret number wuz", secret
raise SystemExit()
import random
secret = random.randint (1, 99)
guess = 0
tries = 0
print "Congratz. You won!"
在提高SystemExit()部分中,我希望人不能夠繼續,如果他們沒有猜到最後一位。它甚至沒有開始,但是當我拿出SystemExit()時,它可以正常工作,但它會繼續下去,即使它們沒有正確猜測。我該怎麼做才能做我想做的事?
你只需要隨機導入一次,通常在腳本的頂部完成。 – Benjamin 2011-02-07 20:46:22
@Benjamin:看起來像很多複製粘貼。 'import`語句並不是唯一可以使用一點重構的位... – 2011-02-07 20:50:31