繁体   English   中英

为什么我的python代码无法正常运行?

[英]Why won't my python code run properly?

我的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()时,它可以工作,但即使他们猜对了,它仍然可以继续。 我该怎么做才能做我想做的?

该行应缩进,使其成为else块的一部分,否则即使猜测正确也将始终执行该行:

raise SystemExit()

最好也改为调用sys.exit()

同样,如果您想做两次,而不是复制和粘贴代码,则应创建一个函数并调用两次:

def play():
    # ...

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

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

import sys
code = 0
sys.exit(code)

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM