我正在撰寫一個程式石頭剪刀布,我有一個回圈問題,而我用另一個回圈解決了這個問題,但我仍然想知道為什么它不起作用(第 19、71、104 行;或者只是在函式游戲中,游戲2,游戲3)。我還想問我是否可以更好地撰寫這個程式或者使用更高級的東西來將它寫得更短。這是代碼:
import random
import time
option = ["rock", "paper", "scissors"]
your_score = 0
computer_score = 0
#functions
def game(your_score, computer_score):
print("ok, lets start")
time.sleep(1)
print(3)
time.sleep(1)
print(2)
time.sleep(1)
print(1)
time.sleep(1)
while your_score != 1 or computer_score != 1:
if your_score == 2 or computer_score == 2:
break
user = int(input("What do you shoot?\n1.rock\n2.paper\n3.scissors\n"))
computer = random.choice([1, 2, 3])
show(user, computer)
if user == computer:
print("it's a tie\n")
elif is_win(user, computer):
print("you won\n")
your_score = 1
else:
print("you lost\n")
computer_score = 1
if your_score == 1:
print("You won, gg")
elif computer_score == 1:
print("You lost, nt")
def show(user, computer):
if user == 1:
print(f"You: {option[0]}")
elif user == 2:
print(f"You: {option[1]}")
elif user == 3:
print(f"You: {option[2]}")
if computer == 1:
print(f"Computer: {option[0]}")
elif computer == 2:
print(f"Computer: {option[1]}")
elif computer == 3:
print(f"Computer {option[2]}")
def game2(your_score, computer_score):
print("ok, lets start")
time.sleep(1)
print(3)
time.sleep(1)
print(2)
time.sleep(1)
print(1)
time.sleep(1)
while your_score != 2 or computer_score != 2:
if your_score == 2 or computer_score == 2:
break
user = int(input("What do you shoot?\n1.rock\n2.paper\n3.scissors\n"))
computer = random.choice([1, 2, 3])
show(user, computer)
if user == computer:
print("it's a tie\n")
elif is_win(user, computer):
print("you won\n")
your_score = 1
else:
print("you lost\n")
computer_score = 1
if your_score == 2:
print("You won, gg")
elif computer_score == 2:
print("You lost, nt")
def game3(your_score, computer_score):
print("ok, lets start")
time.sleep(1)
print(3)
time.sleep(1)
print(2)
time.sleep(1)
print(1)
time.sleep(1)
while your_score != 3 or computer_score != 3:
if your_score == 3 or computer_score == 3:
break
user = int(input("What do you shoot?\n1.rock\n2.paper\n3.scissors\n"))
computer = random.choice([1, 2, 3])
show(user, computer)
if user == computer:
print("it's a tie\n")
elif is_win(user, computer):
print("you won\n")
your_score = 1
else:
print("you lost\n")
computer_score = 1
if your_score == 3:
print("You won, gg")
elif computer_score == 3:
print("You lost, nt")
def is_win(user, computer):
if (user == 1 and computer == 3) or (user == 2 and computer == 1) or (user == 3 and computer ==2):
return True
def start():
s = ()
while s == "yes" or "no":
s = str(input("Are you ready?'yes' or 'no'\n")).lower()
if s == "yes":
game(your_score, computer_score)
break
elif s == "no":
print("ok, I'll wait")
time.sleep(1)
print(3)
time.sleep(1)
print(2)
time.sleep(1)
print(1)
print("You should be ready now")
time.sleep(1)
game(your_score, computer_score)
break
else:
print("wrong insert, try again")
def start2():
s = ()
while s == "yes" or "no":
s = str(input("Are you ready?'yes' or 'no'\n")).lower()
if s == "yes":
game2(your_score, computer_score)
break
elif s == "no":
print("ok, I'll wait")
time.sleep(1)
print(3)
time.sleep(1)
print(2)
time.sleep(1)
print(1)
print("You should be ready now")
time.sleep(1)
game2(your_score, computer_score)
break
else:
print("wrong insert, try again")
def start3():
s = ()
while s == "yes" or "no":
s = str(input("Are you ready?'yes' or 'no'\n")).lower()
if s == "yes":
game3(your_score, computer_score)
break
elif s == "no":
print("ok, I'll wait")
time.sleep(1)
print(3)
time.sleep(1)
print(2)
time.sleep(1)
print(1)
print("You should be ready now")
time.sleep(1)
game3(your_score, computer_score)
break
else:
print("wrong insert, try again")
def gamemode():
g_m = 1
while g_m == 1 or g_m == 2 or g_m == 3:
print("1.The best of 1(up to 1 points)\n")
print("2.The best of 3(up to 2 points)\n")
print("3.The best of 5(up to 3 points)\n")
try:
g_m = int(input("Which one you want to play?\n"))
if g_m == 1:
start()
break
elif g_m == 2:
start2()
break
elif g_m == 3:
start3()
break
else:
print("wrong insert\n")
except ValueError:
print()
print("wrong value, try again\n")
#actual program
gamemode()
我試著弄亂回圈,但它最終沒有作業,所以我只是在里面放了另一個回圈。在我的想法中,回圈應該可以作業,直到這些變數沒有特定的值,但我可能錯了。該程式本身可能看起來很奇怪而且運行時間很長,但它是故意使用time.sleep. 謝謝
uj5u.com熱心網友回復:
通常,您不會制作足夠靈活的功能以進行更改。您應該考慮如何重用組件以減少重復。在那一點上,您也沒有制作足夠的功能并重復很多邏輯。
例如,要從一個數字開始倒數,您可以這樣寫:
def print_countdown(count):
while count > 0:
time.sleep(1)
print(count)
count -= 1
現在,您可以在任何地方使用它來進行倒計時。
為了獲取用戶輸入,您重復了很多 while 回圈邏輯。這可以隔離到它自己的方法中:
def user_choice(prompt, choices):
while True:
choice = input(prompt)
if choice not in choices:
print("Invalid choice try again...")
else:
return choice
舉個例子:
choice = user_choice("Are you ready?'yes' or 'no'\n", ["yes", "no"])
接下來,如果您實際上只是使用了字串值本身,而不是使用選項索引,則列印選項和測驗獲勝的方法變得更加簡單:
def show(user, computer):
print(f"You: {user}")
print(f"Computer: {computer}")
def is_win(user, computer):
return (user, computer) in (
("rock", "scissors"),
("paper", "rock"),
("scissors", "paper"),
)
對于主游戲方法,將用戶的目標分數傳入,并檢查獲勝條件。這將允許您洗掉gameN方法。您可以重復使用您制作的輔助方法。
def game(target_score):
your_score = 0
computer_score = 0
print("ok, lets start")
print_countdown(3)
while your_score != target_score and computer_score != target_score:
user = user_choice("What do you shoot?\nrock\npaper\nscissors\n", option)
computer = random.choice(option)
show(user, computer)
if user == computer:
print("it's a tie\n")
elif is_win(user, computer):
print("you won\n")
your_score = 1
else:
print("you lost\n")
computer_score = 1
if your_score == target_score:
print("You won, gg")
else:
print("You lost, nt")
同樣,這也可以用于其他方法!
def start(target_score):
choice = user_choice("Are you ready?'yes' or 'no'\n", ["yes", "no"])
if choice == "no":
print("ok, I'll wait")
print_countdown(3)
print("You should be ready now")
game(target_score)
def gamemode():
choice = user_choice(
"1.The best of 1(up to 1 points)\n"
"2.The best of 3(up to 2 points)\n"
"3.The best of 5(up to 3 points)\n",
["1", "2", "3"]
)
if choice == "1":
start(1)
elif choice == "2":
start(2)
else:
start(3)
最后確保測驗應用程式是作為腳本呼叫還是作為模塊加載。這將允許您在其他專案中使用游戲方法,而不會通過簡單地匯入模塊而意外啟動游戲
if __name__ == "__main__":
gamemode()
uj5u.com熱心網友回復:
以下是我將如何做一個石頭剪刀布游戲:
import random
options = ["rock", "paper", "scissors"]
your_score = 0
computer_score = 0
max_score = 3
playing = True
while playing:
your_option = input(f"Options:\n - {options[0]}\n - {options[1]}\n - {options[2]}\nYour option: ")
computer_option = random.choice(options)
if your_option.lower() == 'rock':
if computer_option == 'rock':
print('Tie')
elif computer_option == 'paper':
print('Computer wins')
computer_score = 1
elif computer_option == 'scissors':
print('You win')
your_score = 1
elif your_option.lower() == 'paper':
if computer_option == 'rock':
print('You win')
your_score = 1
elif computer_option == 'paper':
print('Tie')
elif computer_option == 'scissors':
print('Computer wins')
computer_score = 1
elif your_option.lower() == 'scissors':
if computer_option == 'rock':
print('Computer wins')
computer_score = 1
elif computer_option == 'paper':
print('You win')
your_score = 1
elif computer_option == 'scissors':
print('Tie')
else:
print("Wrong input!")
# Checking if score is max score
if your_score >= max_score:
print("You won the game")
playing = False
elif computer_score >= max_score:
print("You lost the game")
playing = False
如果您愿意,您可以在代碼的最底部呼叫main()或呼叫一個函式,并且該函式應該包含這個 while 回圈。game()重要的!然后您需要global在開始時使用變數your_score并computer_score更改值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/530442.html
下一篇:加速代碼。用核函式回圈求和
