[英]Program keeps looping in an infinite function?
目前正致力於基礎蟒蛇課的Rock Paper Scissors游戲。 在過去的一周里,我一直在研究它,因為我抬頭看了如何制作一個基本的python游戲,偶然發現了一篇文章並且離開了它。 不知道為什么,但它不斷循環。 我想保持結構與此類似,因為我的導師讓我保持這樣。
我已經嘗試更改cpu_rand和cpu_choice,並將函數results()放在不同的位置,但也無法正常工作。
import random #import random module
import sys #import system module
def playgame():
while True:
player_choice = input("Rock, Paper, or Scissors?")
cpu_rand = random.randint(1,3) #Generate random integer 1-3
cpu_choice = None
if cpu_rand == 1: #if cpu_choice = 1, cpu_choice = "Rock"
cpu_choice = "Rock"
elif cpu_rand == 2: #if cpu_choice = 2, cpu_choice = "Scissors"
cpu_choice = "Scissors"
elif cpu_rand == 3: #if cpu_choice = 3, cpu_choice = "Paper"
cpu_choice = "Paper"
def results(): #check results of player choice v computer choice
play_again = None #Sets this to null for future reference, for asking if playing again.
if(player_choice == cpu_choice): #Tie
print("It's a Tie")
play_again = input("Retry?")
#Rock Outcomes
elif(player_choice == "Rock" and cpu_choice == "Scissors"):
print("You Win!")
play_again = input("Play again?")
elif(player_choice == "Rock" and cpu_choice == "Paper"):
print("You Lose!")
play_again = input("Play again?")
#Paper Outcomes
elif(player_choice == "Paper" and cpu_choice == "Scissors"):
print("You Lose!")
play_again = input("Play again?cpu")
elif(player_choice == "Paper" and cpu_choice == "Rock"):
print("You Win!")
play_again = input("Play again?")
#Scissors Outcomes
elif(player_choice == "Scissors" and cpu_choice == "Rock"):
print("You Lose!")
play_again = input("Play again?")
elif(player_choice == "Scissors" and cpu_choice == "Paper"):
print("You Win!")
play_again = input("Play again?")
if play_again == "Yes": #if elif play again statements, from if/elif statements, play_again is changed to an input
playgame()
elif play_again == "No":
print("You Lose!")
sys.exit()
else:
print("Invalid Command")
play_again = input("play again?")
return play_again
results()
def start():
while True:
gamestart = input("You ready to play some Rock, Paper, Scissors? (y/n)")
if gamestart == "y":
playgame()
return gamestart
elif gamestart == "n":
print("Game Over!")
break
else:
print("Invalid Command")
start()
我希望返回的結果是結果函數下的所有內容,因此如果player_choice == cpu_choice,那么它將打印出其下的內容。 相反,它會回到“搖滾,紙張或剪刀?”
您從未在代碼中調用結果函數,因此它從未運行過。
你沒有調用results()
。 而且player_choice和cpu_choice是playgame()
中的局部變量。
import random #import random module
import sys #import system module
def playgame():
while True:
player_choice = input("Rock, Paper, or Scissors?")
cpu_rand = random.randint(1,3) #Generate random integer 1-3
cpu_choice = None
if cpu_rand == 1: #if cpu_choice = 1, cpu_choice = "Rock"
cpu_choice = "Rock"
elif cpu_rand == 2: #if cpu_choice = 2, cpu_choice = "Scissors"
cpu_choice = "Scissors"
elif cpu_rand == 3: #if cpu_choice = 3, cpu_choice = "Paper"
cpu_choice = "Paper"
results(player_choice, cpu_choice)
def results(player_choice, cpu_choice): #check results of player choice v computer choice
play_again = None #Sets this to null for future reference, for asking if playing again.
if(player_choice == cpu_choice): #Tie
print("It's a Tie")
play_again = input("Retry?")
#Rock Outcomes
elif(player_choice == "Rock" and cpu_choice == "Scissors"):
print("You Win!")
play_again = input("Play again?")
elif(player_choice == "Rock" and cpu_choice == "Paper"):
print("You Lose!")
play_again = input("Play again?")
#Paper Outcomes
elif(player_choice == "Paper" and cpu_choice == "Scissors"):
print("You Lose!")
play_again = input("Play again?cpu")
elif(player_choice == "Paper" and cpu_choice == "Rock"):
print("You Win!")
play_again = input("Play again?")
#Scissors Outcomes
elif(player_choice == "Scissors" and cpu_choice == "Rock"):
print("You Lose!")
play_again = input("Play again?")
elif(player_choice == "Scissors" and cpu_choice == "Paper"):
print("You Win!")
play_again = input("Play again?")
if play_again == "Yes": #if elif play again statements, from if/elif statements, play_again is changed to an input
playgame()
elif play_again == "No":
print("You Lose!")
sys.exit()
else:
print("Invalid Command")
play_again = input("play again?")
return play_again
results()
def start():
while True:
gamestart = input("You ready to play some Rock, Paper, Scissors? (y/n)")
if gamestart == "y":
playgame()
return gamestart
elif gamestart == "n":
print("Game Over!")
break
else:
print("Invalid Command")
start()
你的代碼是如此密集和重復,我可以建議另一種方法嗎?:
import random
wins = [
('paper', 'rock'), # paper wins over rock
('rock', 'scissors'),
('scissors', 'paper'),
]
items = ['rock', 'paper', 'scissors']
def computer_choice():
return random.choice(items)
def player_choice():
for i, item in enumerate(items):
print i, item
while 1:
choice = input("choose [0..%d]: " % (len(items)-1))
if 0 <= choice < len(items):
break
return items[choice]
while 1:
cpu = computer_choice()
human = player_choice()
print "CPU: %s, HUMAN: %s" % (cpu, human)
if cpu == human:
print 'tie..'
elif (cpu, human) in wins:
print "CPU wins."
else:
print "HUMAN wins."
again = raw_input("play again? [q to quit]: ")
print
if again.lower() == 'q':
break
樣本會話:
0 rock
1 paper
2 scissors
choose [0..2]: 0
CPU: scissors, HUMAN: rock
HUMAN wins.
play again? [q to quit]:
0 rock
1 paper
2 scissors
choose [0..2]: 1
CPU: paper, HUMAN: paper
tie..
play again? [q to quit]:
0 rock
1 paper
2 scissors
choose [0..2]: 1
CPU: scissors, HUMAN: paper
CPU wins.
play again? [q to quit]:
使這個玩搖滾,紙,剪刀,蜥蜴,spock( https://bigbangtheory.fandom.com/wiki/Rock,_Paper,_Scissors,_Lizard,_Spock )的唯一變化是:
wins = [
('scissor', 'paper'), # Scissors cuts Paper
('paper', 'rock'), # Paper covers Rock
('rock', 'lizard'), # Rock crushes Lizard
('lizard', 'spock'), # Lizard poisons Spock
('spock', 'scissors'), # Spock smashes Scissors
('scissors', 'lizard'), # Scissors decapitates Lizard
('lizard', 'paper'), # Lizard eats Paper
('paper', 'spock'), # Paper disproves Spock
('spock', 'rock'), # Spock vaporizes Rock
('rock', 'scissors'), # (and as it always has) Rock crushes Scissors
]
items = ['rock', 'paper', 'scissors', 'lizard', 'spock']
樣品運行
0 rock
1 paper
2 scissors
3 lizard
4 spock
choose [0..4]: 4
CPU: scissors, HUMAN: spock
HUMAN wins.
play again? [q to quit]:
0 rock
1 paper
2 scissors
3 lizard
4 spock
choose [0..4]: 2
CPU: spock, HUMAN: scissors
CPU wins.
play again? [q to quit]:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.