-7

The program runs fine. But with this error(screenshot). Its just blank. Its Python 2.7. And I have added Python to Environment Variables as well but nothing shows up on shell as well.

Code for Rock Paper and Scissors

import random
import time

rock = 1
paper = 2
scissors = 3

names = { rock: "Rock" , paper: "Paper" , scissors: "Scissors" }
rules = { rock: scissors , paper :rock , scissors: paper }

player_score = 0
computer_score = 0

def start():

    print "Let's play a game of rock paper and scissors"
    while game():
        pass
    scores()


def game():
    player = move()
    computer = random.randint(1,3)
    result(player, computer)
    return play_again()

def move():
    while True:
        print
        player = raw_int("Rock = 1\nPaper = 2\nScissors =3\nMake a move: ")

        try:
            player = int(player)
            if player in (1,2,3):
                return player
        except ValueError:
            pass
            print "Oops! I didn't understand that. Please enter 1,2 or 3."

def result(player, computer):
    print "1..."
    time.sleep(1)
    print "2..."
    time.sleep(1)
    print "3..."
    time.sleep(0.5)

    print "Computer threw {0)!".format(names[computer])

    global player_score,computer_score

    if player == computer:
        print "Tie game."


    else:
        if rules[player] == computer:
            print "Your victory has been assured."
            player_score += 1

        else:
            print" The computer laughs as you realise you have been defeated."
            computer_score += 1


        def play_again():
            answer = raw_input("Would you like to play again? y/n: ")
            if answer in ("Y", "Y" , "yes" , "Yes" , "Of course!"):
                return answer
            else:
                print "Thank you very much for playing our game.See your next time!"

        def scores():
            global player_score,computer_score
            print "High Scores"
            print "Player:" , player_score
            print "Computer:", computer_score

        if _name_ == '_main_':
          start()

Error:

Traceback (most recent call last): File "C:/Users/Sarthak/Desktop/RPS.py", line 80, in if name == 'main': NameError: name 'name' is not defined

Matt
  • 74,352
  • 26
  • 153
  • 180
Sarthak Bhatia
  • 104
  • 1
  • 10
  • 1
    Your `if __name__ == '__main__'` is not indented correctly, right now it is in the scope of your `scores` function – MaxNoe Mar 10 '16 at 07:21
  • @MaxNoe Thanks now it runs but a traceback error. Added screenshot above. – Sarthak Bhatia Mar 10 '16 at 07:32
  • Traceback (most recent call last): File "C:/Users/Sarthak/Desktop/RPS.py", line 80, in if _name_ == '_main_': NameError: name '_name_' is not defined – Sarthak Bhatia Mar 10 '16 at 07:41
  • 1
    "please suggest a..." --- This question is completely unrelated to your primary question. Normally, I would tell you to create a new question for each new topic, but _this_ kind of question is specifically listed as _off_-topic in the [Help Center](https://stackoverflow.com/help)'s "[What topics can I ask about here?](https://stackoverflow.com/help/on-topic)" page (banned topic #4), so just delete it. [Googling "Python tutorial"](https://encrypted.google.com/search?q=Python+tutorial) will tell you exactly where to go. – Kevin J. Chase Mar 10 '16 at 09:07
  • 2
    Please do not edit your question to include a new problem. Instead, create a new question. I would also advise you read http://stackoverflow.com/help/how-to-ask before asking your next question, as you seem to have not grasped some of the rules around asking questions on Stack Overflow. – Matt Mar 11 '16 at 13:01

1 Answers1

-2

You should fix your indentations and all other errors:

import random
import time

rock = 1
paper = 2
scissors = 3

names = { rock: "Rock" , paper: "Paper" , scissors: "Scissors" }
rules = { rock: scissors , paper :rock , scissors: paper }

player_score = 0
computer_score = 0

def start():

    print "Let's play a game of rock paper and scissors"
    while game():
        pass
    scores()


def game():
    player = move()
    computer = random.randint(1,3)
    result(player, computer)
    return play_again()

def move():
    while True:
        print #this is not how you could get int input in Python
        player = raw_int("Rock = 1\nPaper = 2\nScissors =3\nMake a move: ")

        try:
            player = int(player)
            if player in (1,2,3):
                return player
        except ValueError:
            pass
        print "Oops! I didn't understand that. Please enter 1,2 or 3." #note the indentation here

def result(player, computer):
    print "1..."
    time.sleep(1)
    print "2..."
    time.sleep(1)
    print "3..."
    time.sleep(0.5)

    print "Computer threw {0)!".format(names[computer])

    global player_score,computer_score

    if player == computer:
        print "Tie game."


    else:
        if rules[player] == computer:
            print "Your victory has been assured."
            player_score += 1

        else:
            print" The computer laughs as you realise you have been defeated."
            computer_score += 1


def play_again(): #again, indentation
    #and this is not how you could get string input in Python
    answer = raw_input("Would you like to play again? y/n: ")
    if answer in ("Y", "Y" , "yes" , "Yes" , "Of course!"):
        return answer
    else:
        print "Thank you very much for playing our game.See your next time!"

def scores(): #note the indentation here
    global player_score,computer_score
    print "High Scores"
    print "Player:" , player_score
    print "Computer:", computer_score

if __name__ == '__main__': #note the underscores here
  start()
Ian
  • 30,182
  • 19
  • 69
  • 107
  • I am new to Python. How to call start()? And I did called it. But then there is this error uni dented doesn't match the outer indentation level – Sarthak Bhatia Mar 10 '16 at 07:19
  • 1
    As the person who answered showed, you can just stick `start()` at the bottom of your program. – BHouwens Mar 10 '16 at 07:22
  • @BHouwens I did stick it at the bottom. But then there is an error Unidented doesn't match outer identation level. – Sarthak Bhatia Mar 10 '16 at 07:23
  • @SarthakBhatia call it at the bottom of your program and *don't* give *any* indentation at all – Ian Mar 10 '16 at 07:24
  • @BHouwens Now there is a Traceback (most recent call last): File "C:/Users/Sarthak/Desktop/RPS.py", line 80, in if _name_ == '_main_': NameError: name '_name_' is not defined – Sarthak Bhatia Mar 10 '16 at 07:38
  • @SarthakBhatia same issue your `def play_again()` and `def scores()` should have no indentation at all – Ian Mar 10 '16 at 07:41
  • @SarthakBhatia that is because there are multiple errors in your code. Also, you should put it with `double underscore` rather than `single`: `if __name__ == '__main__':` – Ian Mar 10 '16 at 07:49
  • @Ian Thanks. & now these errors Traceback (most recent call last): File "C:/Users/Sarthak/Desktop/RPS.py", line 81, in start() File "C:/Users/Sarthak/Desktop/RPS.py", line 17, in start while game(): File "C:/Users/Sarthak/Desktop/RPS.py", line 23, in game player = move() File "C:/Users/Sarthak/Desktop/RPS.py", line 31, in move player = raw_int("Rock = 1\nPaper = 2\nScissors =3\nMake a move: ") NameError: global name 'raw_int' is not defined – Sarthak Bhatia Mar 10 '16 at 07:54
  • @Ian Thanks. Now it runs perfectly. Hey I am aspiring to be a data scientist. Can you suggest some books,tuts. That I should follow to take that path. – Sarthak Bhatia Mar 10 '16 at 08:18
  • @SarthakBhatia you mean about Python? Hmm... there is this website. Learning Python the Hard Way: http://learnpythonthehardway.org/, which I think is quite good. You can take a look on that. Also, you may want to learn how to use `numpy`, a very good Python tool to deal with numbers. – Ian Mar 10 '16 at 08:42