代码运行但回答不正确(python)

时间:2013-04-25 11:09:46

标签: python

player_number=name_to_number(name)
player_number=int ()

# compute random guess for comp_number using random.randrange()
comp_number=random.randrange (0,4)

# compute difference of player_number and comp_number modulo five
rpsls_match = (player_number-comp_number)%5

 # use if/elif/else to determine winner
if rpsls_match == 1 or 2:
    rpsls_winner = 'Player wins!'
elif rpsls_match == 3 or 4:
    rpsls_winner = 'Computer wins!'
else:
    rpsls_winner = 'Player and computer tie!'

    #print "Player chooses " + name

   # convert comp_number to name using number_to_name
print 'Computer chooses ', number_to_name(comp_number),comp_number
print 'player chooses ' , name, player_number
return rpsls_winner

代码运行正常,但我没有得到我想要的答案,它给我答案像

Computer chooses  paper 2
player chooses  rock 0
Player wins!

Computer chooses  lizard 3
player chooses  paper 0
Player wins!

Computer chooses  spock 1
player chooses  scissors 0
Player wins!

Computer chooses  paper 2
player chooses  lizard 0
Player wins!

incorrect input
Computer chooses  lizard 3
player chooses  Spock 0
Player wins!

但我想要

Player chooses rock
Computer chooses scissors
Player wins!

Player chooses Spock
Computer chooses lizard
Computer wins!

Player chooses paper
Computer chooses lizard
Computer wins!

Player chooses lizard
Computer chooses scissors
Computer wins!

Player chooses scissors
Computer chooses Spock
Computer wins!

2 个答案:

答案 0 :(得分:3)

rpsls_match == 1 or 2应该是

rpsls_match in (1, 2)

rpsls_match == 1 or rpsls_match == 2

尝试在REPL评估1 or 2。答案是1,因为or返回第一个非错误评估操作数。

答案 1 :(得分:2)

该行

player_number=int ()

始终将player_number设置为int的默认值,即零。您应该删除该行以保持在上一行中设置的值。