为什么我的python代码不能正常运行?

时间:2011-02-07 20:39:47

标签: 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()时,它可以工作,但它会继续运行,即使它们没有正确猜测它。我该怎么做才能做我想做的事?

3 个答案:

答案 0 :(得分:4)

这一行应该缩进,以便它是else块的一部分,否则即使猜测正确也会一直执行:

raise SystemExit()

也可以更好地致电sys.exit()

此外,如果你想做两次,而不是复制和粘贴代码,你应该创建一个函数并调用它两次:

def play():
    # ...

number_of_games = 2
for i in range(number_of_games):
    play()

答案 1 :(得分:2)

要在Python中退出脚本,您只想使用sys.exit

import sys
code = 0
sys.exit(code)

代码是脚本的返回代码。如果您的脚本没有错误,它应该为零,否则为1到127之间的任何数字。用户错误不是脚本错误,因此如果您的用户不猜,只需输入0。

答案 2 :(得分:0)

您应该尝试创建一个函数,而不要过多地复制粘贴,否则可能会解决部分问题。我认为其中一些代码已经过时了...

我们要将其放在以下while循环中:

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()

在其上方的while循环中:

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

因此使用此代码创建一个名为guess_check()的函数

例如:

secret = 3
tries = 0
guess = 0

def guess_check():
    if int(guess) < secret:
        print ("Too low, ye scurvy dog!")
    elif int(guess) > secret:
        print ("Too high, landlubber!")
    elif int(guess) == secret:
        print ("Avast! Ye got it! Found ma secret number, ye did!")
        print ("THE FIRST WORD IS: Awesome")
    elif tries == 6:
        print ("No more guesses! Better luck next time, Matey!")
        print ("Ma secret number wuz", secret)


while guess != secret and tries < 6:
    guess = input ("What's yer guess? ")
    tries = tries + 1
    guess_check();
    if guess == secret:
        break
    elif tries == 6:
        break
    else:
        pass

此代码只是一个起点,您需要对其进行调整以满足您的需求,我不是在为您做业余爱好:),但希望对您有所帮助!