-1
我在很多地方都在這個網站上搜索過,但無法理解它,因爲當我試圖將它實現到我的代碼中時,它不起作用。在函數之間傳遞變量
下面是完整的代碼。
from random import randint
from time import sleep
def keepscore():
score = 0
cscore = 0
return score, cscore
def gamestart():
print("""Starting in:
_____
|___/
|_ \
___) | _ _ _
|____/ (_) (_) (_)
""")
sleep(1)
print("""
____
|___ \
__) |
/__/ _ _ _
|_____| (_) (_) (_)
""")
sleep(1)
print("""
_
/|
| |
| | _ _ _
|_| (_) (_) (_)
""")
sleep(1)
play()
def play():
score,cscore = keepscore()
rps = ["Rock","Paper","Scissors"]
cpu = rps[randint(0,2)]
ask = input("Rock, Paper, Scissors? ")
if ask == "Rock":
if cpu == "Rock":
sleep(0.5)
print("\nThe computer chose Rock!")
print("It's a tie! Your score is: " + str(score) + ". The computer's score is: " + str(cscore) + ".\n")
elif cpu == "Paper":
sleep(0.5)
print("\nThe computer chose Paper!")
print("You lose!")
cscore += 1
print("Your score is: " + str(score) + ". The computer's score is: " + str(cscore) + ".\n")
elif cpu == "Scissors":
sleep(0.5)
print("\nThe computer chose Scissors!")
print("You win!")
score += 1
print("Your score is: " + str(score) + ". The computer's score is: " + str(cscore) + ".\n")
elif ask == "Paper":
if cpu == "Rock":
sleep(0.5)
print("\nThe computer chose Rock!")
print("You lose!")
cscore += 1
print("Your score is: " + str(score) + ". The computer's score is: " + str(cscore) + ".\n")
elif cpu == "Paper":
sleep(0.5)
print("\nThe computer chose Paper!")
print("It's a tie! Your score is: " + str(score) + ". The computer's score is: " + str(cscore) + ".\n")
elif cpu == "Scissors":
sleep(0.5)
print("\nThe computer chose Scissors!")
print("You win!")
score += 1
print("Your score is: " + str(score) + ". The computer's score is: " + str(cscore) + ".\n")
elif ask == "Scissors":
if cpu == "Rock":
sleep(0.5)
print("\nThe computer chose Rock!")
print("You lose!")
cscore += 1
print("Your score is: " + str(score) + ". The computer's score is: " + str(cscore) + ".\n")
elif cpu == "Paper":
sleep(0.5)
print("\nThe computer chose Paper!")
print("You win!")
score += 1
print("Your score is: " + str(score) + ". The computer's score is: " + str(cscore) + ".\n")
elif cpu == "Scissors":
sleep(0.5)
print("\nThe computer chose Scissors!")
print("It's a tie! Your score is: " + str(score) + ". The computer's score is: " + str(cscore) + ".\n")
else:
print("That wasn't a valid option. Please try again.\n")
return score,cscore
def best():
bestof = input("Best of: 1, 2, 3, 4, or 5? ")
if bestof == "1":
for i in range(1):
gamestart()
elif bestof == "2":
for i in range(2):
gamestart()
elif bestof == "3":
for i in range(3):
gamestart()
elif bestof == "4":
for i in range(4):
gamestart()
elif bestof == "5":
for i in range(5):
gamestart()
elif int(bestof) >= 5:
sleep(0.5)
print("...")
sleep(1)
print("Really?... Fine.\n")
sleep(2)
for i in range(int(bestof)):
gamestart()
else:
print("Defaulting to 3...")
for i in range(3):
gamestart()
keepscore()
best()
我想要做的是使循環的每次迭代後的比分停留,但它每次重置回0。 我已經嘗試了多種方法,包括while循環和全局變量設置。
請鏈接到沒有意義或在你的代碼沒有工作的問題,我可以找到大量的資源,解釋如何改變'global'變量在功能上。 –
也許看到https://docs.python.org/3/faq/programming.html#what-are-the-rules-for-local-and-global-variables-in-python –