1 Replies - 389 Views - Last Post: 17 October 2013 - 08:46 PM Rate Topic: -----

#1 junior6202  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 15-May 13

I need guidance

Posted 17 October 2013 - 08:41 PM

I'm a beginner in python and I'm doing a beginners project and i keep getting this error on my code.
"Line 71: TypeError: unsupported operand type(s) for Sub: 'NoneType' and 'int'"
# Rock-paper-scissors-lizard-Spock template



# The key idea of this program is to equate the strings
# "rock", "paper", "scissors", "lizard", "Spock" to numbers
# as follows:
#
# 0 - rock
# 1 - Spock
# 2 - paper
# 3 - lizard
# 4 - scissors

# helper functions
import random

def number_to_name(number):
    # fill in your code below
    
    # convert number to a name using if/elif/else
    # don't forget to return the result!
    if(number == 0): 
        result = "Rock"
        
    elif(number == 1): 
        result = "Spock"
        
    elif(number == 2): 
        result = "Paper"
        
    elif(number == 3): 
        result = "Lizard"
        
    elif(number == 4): 
        result = "Scissors"    
    
def name_to_number(name):
    # fill in your code below

    # convert name to number using if/elif/else
    # don't forget to return the result!
    
    if (name == "Rock"): 
        result = 0
        
    elif (name == "Spock"): 
        result = 1
        
    elif(name == "Paper"): 
        result = 2
        
    elif(name == "Lizard"): 
        result = 3
        
    elif(name == "Scissors"): 
        result = 4


def rpsls(name):
    
    
    # fill in your code below

    # convert name to player_number using name_to_number
    player_number = name_to_number(name)
    
    # compute random guess for comp_number using random.randrange()
    comp_number = random.randrange(0,5)
    # compute difference of player_number and comp_number modulo five
    diff = (player_number - comp_number) % 5  <-------------------------------------------------ERROR
    # use if/elif/else to determine winner
    if (diff == 1):
        print "Player wins!"
    elif (diff == 2):
        print "Player wins!"
    elif (diff == 3):
        print "Computer wins!"
    elif (diff == 4):
        print "Computer wins!" 
    else:
        print "Tie!"
    # convert comp_number to name using number_to_name
    
    # print results
    
      
     
    print "Player chooses: ", number_to_name(name)
    print "Computer chooses: " ,number_to_name(comp_number)
  
    
    
   
    
    

    
# test your code
rpsls("rock")
rpsls("Spock")
rpsls("paper")
rpsls("lizard")
rpsls("scissors")   

This post has been edited by jon.kiparsky: 17 October 2013 - 08:43 PM


Is This A Good Question/Topic? 0
  • +

Replies To: I need guidance

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7737
  • View blog
  • Posts: 13,070
  • Joined: 19-March 11

Re: I need guidance

Posted 17 October 2013 - 08:46 PM

Your name_to_number function doesn't return the result that it calculates, so the result of evaluating it here
player_number = name_to_number(name)


is a None. You can't subtract an int from an None, so you get the error.

If you add the return to the function, it should work fine.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1