ホームページ  >  に質問  >  本文

python - TypeError: unsupported operand type(s) for Sub: 'str' and 'int'

import random
def number_to_name(number):
if number==0:
return "rock"
elif number==1:
return "Spock"
elif number==2:
return "paper"
elif number==3:
return "lizard"
elif number==4:
return "scissor"
else:
return "wrong number you have entered"
def name_to_number(name):
if name=="rock":
return 0
elif name=="Spock":
return 1
elif name=="paper":
return 2
elif name=="lizard":
return 3
elif name=="scissor":
return 4
else:
return "wrong name you have entered"

define the number to name function

define compare function

def rpsls(player_choice):

print "the player choice is "+player_choice

player_number=name_to_number(player_choice)

computer_number=random.randrange(0,5)

computer_choice=number_to_name(computer_number)

print "the computer choice is "+computer_choice

diff = (player_number - computer_number) % 5

if diff==1 or diff==2:
    print "player wins"
elif diff==3 or diff==4:
    print "computer wins"
else:
    print "ties"    
print ""

rpsls("rock")
rpsls("Spock")
rpsls("paper")
rpsls("lizard")
rpsls("scissors")
总是提示diff = (player_number - computer_number) % 5这个操作式中存在int与str不能相减的问题,但是我没有发现问题啊

ringa_leeringa_lee2765日前691

全員に返信(1)返信します

  • PHP中文网

    PHP中文网2017-04-17 15:19:30

    リーリー リーリー

    はさみを入力して「入力した名前が間違っています」を返します。

    返事
    0
  • キャンセル返事