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

1 投票
3 回答
2067 浏览
提问于 2025-04-16 11:24

我的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!"

在raise SystemExit()这一部分,我希望如果他们没有猜对最后一部分,就不能继续。但是代码根本不运行,当我把raise SystemExit()去掉时,代码可以运行,但即使他们没有猜对也会继续进行。我该用什么来实现我想要的效果呢?

3 个回答

0

你应该试着创建一个函数,而不是复制粘贴这么多代码,这样可能会解决部分问题。我觉得这些代码有些过时了……

我们想把这个放到下面的循环里:

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

这段代码只是一个起点,你需要根据自己的需求进行调整,我可不想替你做你的爱好 :) 但希望这能帮到你!!

2

要在Python中退出一个脚本,你只需要使用 sys.exit

import sys
code = 0
sys.exit(code)

这里的代码是你脚本的返回码。如果你的脚本没有出错,这个返回码应该是零;如果有错误,返回码可以是1到127之间的任意数字。用户的错误不算脚本的错误,所以如果用户没有猜对,你可以直接返回0。

4

这一行代码应该缩进,这样它才能成为else块的一部分。否则,即使猜测是正确的,这行代码也会一直执行。

raise SystemExit()

另外,可能更好的是直接调用 sys.exit()

如果你想做同样的事情两次,最好不要复制粘贴代码,而是创建一个函数,然后调用这个函数两次:

def play():
    # ...

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

撰写回答