[英]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.