为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

明月笑刀无情 2019-02-21 04:13:33
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不能相减的问题,但是我没有发现问题啊
查看完整描述

1 回答

?
浮云间

TA贡献1829条经验 获得超4个赞

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"
rpsls("scissors")

传入scissors,返回的是“wrong name you have entered”。

查看完整回答
反对 回复 2019-03-01
  • 1 回答
  • 0 关注
  • 1052 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信