我正在尝试完成这个程序的帮助。这件事情有点乱。它结束了12小时的一天,它将在今晚到期,我想我现在只是让事情变得更糟。我正在努力为player_choice实现“4”的输入并让程序只是说'退出程序'并停止。当我输入当前的选择时,我得到了我认为无限循环,因为整个IDE锁定并崩溃。
计划要求:
1) Get and return the value of the computer’s choice as an integer.
2) Use a menu to get, validate, and return the player’s choices.
The player will enter their menu choice for rock, paper, scissors, or quit.
Validate the choice before returning it.
3) Determine the winner. There are a total of 9 possible combinations of
computer and player choices. The menu should always be displayed after the outcome has
been displayed regardless of the outcome
(i.e. ties should be treated the same as any other outcome).
4) After the player chooses to stop playing, display the total number of
games that ended in a tie, the total number the computer won, and the total
number the player won.
以下是程序代码:
import random
def main():
display_menu()
print('There were', number_of_tied_games, 'tie games played.')
print('The computer won', number_of_computer_games, 'game(s).')
print('You won', number_of_player_games, 'game(s).')
def process_computer_choice():
choice1 = random.randint(1,3)
return choice1
def process_player_choice():
print('What is your choice?')
choice2 = (input())
while choice2 != "1" and choice2 != "2" and choice2 != "3" and choice2 != "4":
print("ERROR: the choice can only be 1, 2, 3, or 4.")
choice2 = (input("Please enter a correct choice: "))
return choice2
def determine_winner(player_choice, computer_choice, choice2):
while choice2 != "4":
if computer_choice == 1:
if player_choice == "2":
print('Paper covers rock. You win!')
winner = 'player'
elif player_choice == "3":
print("Rock crushes scissors. The computer wins!")
winner = 'computer'
else:
print('The game is tied. Try again.')
winner = 'tied'
if computer_choice == 2:
if player_choice == "1":
print('Paper covers rock. The computer wins!')
winner = 'computer'
elif player_choice == "3":
print("Scissors cuts paper. You win!")
winner = 'player'
else:
print('The game is tied. Try again.')
winner = 'tied'
if computer_choice == 3:
if player_choice == "1":
print('Rock smashes scissors. You win!')
winner = 'player'
elif player_choice == "2":
print("Scissors cuts paper. The computer wins!")
winner = 'computer'
else:
print('The game is tied. Try again.')
winner = 'tied'
return winner
def display_menu():
choice2 = 0
number_of_tied_games = 0
number_of_player_games = 0
number_of_computer_games = 0
print(' MENU')
print('1) Rock')
print('2) Paper')
print('3) Scissors')
print('4) Quit')
print("Let's play the game of Rock, Paper, Scissors.")
computer_choice = process_computer_choice()
player_choice = process_player_choice()
while choice2 != "4":
if computer_choice == 1:
print('The computer chooses rock.')
elif computer_choice == 2:
print('The computer chooses paper.')
else:
print('The computer chooses scissors.')
#display player choice
if player_choice == "1":
print('You choose rock.')
elif player_choice == "2":
print('You choose paper.')
else:
print('You choose scissors.')
result = determine_winner(player_choice, computer_choice, choice2)
if result == 'computer':
number_of_computer_games += 1
elif result == 'player':
number_of_player_games += 1
else:
number_of_tied_games += 1
print
main()
答案 0 :(得分:5)
您的程序将无限运行,因为您没有更新choice2
函数内的变量display_menu()
。你在做什么:
choice2 = 0
while choice2 != "4":
# your code
result = determine_winner(player_choice, computer_choice, choice2)
所以,最终choice2
始终为0,而你的while循环一直无休止地运行。
修改:您将从process_player_choice()
返回一个值,并将值分配给player_choice
函数内的变量display_menu()
。
player_choice = process_player_choice()
我猜你应该将返回值赋给变量choice2
,这样当用户提供4
作为输入时,程序将被终止。
choice2 = process_player_choice()
请注意,determine_winner()
中有另一个while循环,由于choice2
,它也会无休止地运行。我相信while循环不是必需的,因为你已经在display_menu()
函数中有了一个while循环。
因此,简而言之,您实际上并不需要额外的变量player_choice
,您只需要一个存储用户选择的变量,并将其传递给所需的函数来执行其操作。您还应该省略display_menu()
函数的while循环。
最后,正如@AdrianoMartins在他的回答中提到的,您在display_menu()
中声明的以下变量应该是全局的,否则您将无法从main()
函数访问它们。 / p>
number_of_tied_games = 0
number_of_player_games = 0
number_of_computer_games = 0
您可以在全局范围内声明它们,然后在display_menu()
内将它们声明为全局,以修改它们的值。例如:
// declaring globally
tied_games = 0
player_games = 0
computer_games = 0
def display_menu():
// need to modify global copy of the variables
global tied_games
global player_games
global computer_games
要了解详情,建议您查看SO post。
答案 1 :(得分:2)
该问题与determine_winner
方法有关。
方法内部的代码将在choice2
的值不是4时运行,但您不会在循环期间更改它,因此它不会停止循环。
这个问题有几个解决方案,最简单的就是不使用循环(毕竟,你应该在每场比赛中只检查一次结果)。
def determine_winner(player_choice, computer_choice, choice2):
if computer_choice == 1:
if player_choice == "2":
print('Paper covers rock. You win!')
winner = 'player'
elif player_choice == "3":
print("Rock crushes scissors. The computer wins!")
winner = 'computer'
else:
print('The game is tied. Try again.')
winner = 'tied'
if computer_choice == 2:
if player_choice == "1":
print('Paper covers rock. The computer wins!')
winner = 'computer'
elif player_choice == "3":
print("Scissors cuts paper. You win!")
winner = 'player'
else:
print('The game is tied. Try again.')
winner = 'tied'
if computer_choice == 3:
if player_choice == "1":
print('Rock smashes scissors. You win!')
winner = 'player'
elif player_choice == "2":
print("Scissors cuts paper. The computer wins!")
winner = 'computer'
else:
print('The game is tied. Try again.')
winner = 'tied'
return winner
也就是说,还有其他错误需要修复:
player_choice
。 (@lines 62~74)choice2
变量后,您无需将其传递给determine_winner方法。完整的完整代码是:
import random
tied_games = 0
player_games = 0
computer_games = 0
def main():
display_menu()
print('There were', tied_games, 'tie games played.')
print('The computer won', computer_games, 'game(s).')
print('You won', player_games, 'game(s).')
def process_computer_choice():
choice1 = random.randint(1,3)
return choice1
def process_player_choice():
print('What is your choice?')
choice2 = (input())
while choice2 != "1" and choice2 != "2" and choice2 != "3" and choice2 != "4":
print("ERROR: the choice can only be 1, 2, 3, or 4.")
choice2 = (input("Please enter a correct choice: "))
return choice2
def determine_winner(player_choice, computer_choice):
if computer_choice == 1:
if player_choice == "2":
print('Paper covers rock. You win!')
winner = 'player'
elif player_choice == "3":
print("Rock crushes scissors. The computer wins!")
winner = 'computer'
else:
print('The game is tied. Try again.')
winner = 'tied'
if computer_choice == 2:
if player_choice == "1":
print('Paper covers rock. The computer wins!')
winner = 'computer'
elif player_choice == "3":
print("Scissors cuts paper. You win!")
winner = 'player'
else:
print('The game is tied. Try again.')
winner = 'tied'
if computer_choice == 3:
if player_choice == "1":
print('Rock smashes scissors. You win!')
winner = 'player'
elif player_choice == "2":
print("Scissors cuts paper. The computer wins!")
winner = 'computer'
else:
print('The game is tied. Try again.')
winner = 'tied'
return winner
def display_menu():
global tied_games
global player_games
global computer_games
player_choice = 0
while player_choice != "4":
print(' MENU')
print('1) Rock')
print('2) Paper')
print('3) Scissors')
print('4) Quit')
print("Let's play the game of Rock, Paper, Scissors.")
computer_choice = process_computer_choice()
player_choice = process_player_choice()
if player_choice != "4":
if computer_choice == 1:
print('The computer chooses rock.')
elif computer_choice == 2:
print('The computer chooses paper.')
else:
print('The computer chooses scissors.')
#display player choice
if player_choice == "1":
print('You choose rock.')
elif player_choice == "2":
print('You choose paper.')
else:
print('You choose scissors.')
result = determine_winner(player_choice, computer_choice)
if result == 'computer':
computer_games += 1
elif result == 'player':
player_games += 1
else:
tied_games += 1
if __name__ == '__main__':
main()
我相信这一切:)