[英]Why won't my python code run properly?
My python code is the following but it won't work. 我的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!"
In the raise SystemExit() part, I want the person to not be able to continue if they didn't guess the last bit. 在抬高SystemExit()部分中,我希望该人如果没有猜到最后一点就无法继续。 It doesn't even start, but when I take out the raise SystemExit(), it works but it keeps going, even if they didn't guess it correctly. 它甚至还没有开始,但是当我拿出抬高的SystemExit()时,它可以工作,但即使他们猜对了,它仍然可以继续。 What do I put instead to do what I want? 我该怎么做才能做我想做的?
This line should be indented so that it is part of the else block, otherwise it will always be executed even if the guess is correct: 该行应缩进,使其成为else块的一部分,否则即使猜测正确也将始终执行该行:
raise SystemExit()
It might also be better to call sys.exit()
instead. 最好也改为调用sys.exit()
。
Also if you want to do something twice, instead of copying and pasting your code you should create a function and call it twice: 同样,如果您想做两次,而不是复制和粘贴代码,则应创建一个函数并调用两次:
def play():
# ...
number_of_games = 2
for i in range(number_of_games):
play()
To exit a script in Python, you just want to use sys.exit
: 要退出Python中的脚本,您只想使用sys.exit
:
import sys
code = 0
sys.exit(code)
Code is the return code of your script. 代码是脚本的返回代码。 It should be zero if your script made no error, any number from 1 to 127 otherwise. 如果脚本没有错误,则应该为零,否则为1到127之间的任何数字。 User error is not a script error, so if your user don't guess, just put 0. 用户错误不是脚本错误,因此,如果您的用户不猜,则只需输入0。
You should try to create a function instead of copy-pasting so much, that might resolve part of the issue. 您应该尝试创建一个函数,而不要过多地复制粘贴,这可能会解决部分问题。 Some of this code is outdated I think... 我认为其中一些代码已经过时了...
We want to put this in the while loop below: 我们想把它放在下面的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()
This in the while loop above it: 在它上面的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
So make a function called guess_check() with this code 因此,使用此代码创建一个名为guess_check()的函数
Eg: 例如:
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
This code is just a starting point, You will need to tweak it to fit your needs, i'm not doing your hobby for you :) but hope this helps!! 这段代码只是一个起点,您需要对其进行调整以满足您的需求,我不是在为您做业余爱好:),但希望对您有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.