苦苦挣扎的Python Newb。剪刀石头布

时间:2015-10-21 13:43:50

标签: python

简单的RPS程序。程序现在正在运行,除了再次播放的提示,但之后它会抛出错误。这些是我回来的错误:

   Traceback (most recent call last):
   File "f:\programming class\homework\DyerS#6.py", line 117, in <module>
    main()
   File "f:\programming class\homework\DyerS#6.py", line 29, in <module>
    play_game()
   File "f:\programming class\homework\DyerS#6.py", line 58, in <module>
    winner=determine_the_winner(computer_choice,player_choice)
   File "f:\programming class\homework\DyerS#6.py", line 102, in <module>
    return winner
   builtins.NameError: name 'winner' is not defined

代码:

#Imports the random module
import random

#Displays the welcome message, uses a while loop to determine if the
#player wishes to continue
def main():
#Displays the welcome message
print("Welcome to the most intense, and exciting game of rock, paper,\
scissors you have ever played.  People with heart conditions, small\
children, and the elderly should proceed with caution!!!!")

#create a variable for the control loop
keep_going = "y"

#Play the game and determine whether or not the player wishes to continue
while keep_going == "y" or keep_going == "Y":

    #use a function to play the game
    play_game()

    #Determine if they wish to play again
    keep_going = input("Do you wish to play again?(enter y to continue):")

#play_game function will call a function to get a randomly generated number
#for the computers choice. Call a function to get and return the players   choice.
#Use print statements to display the computers choice. Call a function to 
#determine the winner

def play_game():

#call a function to generate the computers choice
computer_choice=get_computer_choice()

#call a function to get the players choice
player_choice=get_players_choice()

#Print statements to display the computers choice
if computer==1:
    print("The almighty computer has chosen ROCK!!!")
elif computer==2:
    print("The almighty computer has chosen PAPER!!!")
elif computer==3:
    print("The almighty computer has chosen SCISSORS!!!")
else:
    print("Stupid computer. The choices are 1,2,3")

#call a function to determine the winner
winner=determine_the_winner(computer_choice,player_choice)


#Generate a random integer for the omputers choice and return that
#Value
def get_computer_choice():
computer_choice = random.randint(1,3)
return computer_choice

#Use an input statement to get the players choice, use a while statement
#to determine whether the choice is valid. Return the value.

def get_players_choice():
print("Select a choice: \n (1):Rock \n (2): Paper \n (3):Scissors")
player_choice=input("Please enter a menu selection:")
while player_choice >3 or player_choice < 1:
    print("ERROR!!! PLease make a valid menu selection!!!")
    player_choice = int(input("Enter a correct selection please:"))
return player_choice

#Use if ,elif,else statements to detrmine the winner and return the value

def determine_the_winner(computer_choice,player_choice):
if player_choice ==1:
    print("You have chosen Rock!")
    if computer_choice ==1:
        print("Computer has chosen rock as well. TIE!")
        return winner
    elif computer_choice ==2:
        print("The computer has chosen paper. Paper covers rock! You LOSE!")
        return winner
    else:
        print("The computer has cosen scissors. Rock breaks Scissors. You WIN!!!")
        return winner
elif player_choice == 2:
    print("You have chosen Paper.")
    if computer_choice==1:
        print("The computer has chosen Rock! Paper covers Rock. You Win!!!")
        return winner
    elif computer_choice == 2:
        print("The computer has chosen Paper as well. TIE!!!")
        return winner
    else:
        print("The computer has chosen Scissors. Scissors cut Paper! You LOSE!!!!")
        return winner

else:
    print("You have chosen Scissors!")
    if computer_choice ==1:
        print("The computer has chosen Rock. Rock breaks Scissors! You LOSE!!!")
        return winner

    elif computer_choice == 2:
        print("The computer has chosen Paper. Scissors cut Paper! You WIN!!!")
        return winner
    else:
        print("The computer has chosen Scissors as well. TIE!!!")
        return winner

main()   

欢迎任何有用的输入!

1 个答案:

答案 0 :(得分:1)

player_choice=input("Please enter a menu selection:")
while player_choice >3 or player_choice < 1:

在Python 3.X中,input返回一个字符串,您无法将字符串与整数进行比较。在分配之前将值转换为int。

player_choice=int(input("Please enter a menu selection:"))
while player_choice >3 or player_choice < 1: