player1 = raw_input("Player 1? ")
player2 = raw_input("Player 2? ")
if ((player1 != "rock" or
player1 != "paper" or
player1 != "scissors") and
(player2 != "rock" or
player2 != "paper" or
player2 != "scissors")):
if (player1 == 'rock' and player2 == 'rock'):
print 'Tie.'
if (player1 == 'paper' and player2 == 'paper'):
print 'Tie.'
if (player1 == 'scissors' and player2 == 'scissors'):
print 'Tie.'
if (player1 == 'paper' and player2 == 'rock'):
print 'Player 1 wins.'
if (player1 == 'rock' and player2 == 'scissors'):
print 'Player 1 wins.'
if (player1 == 'scissors' and player2 == 'paper'):
print 'Player 1 wins.'
if (player1 == 'rock' and player2 == 'paper'):
print 'Player 2 wins.'
if (player1 == 'scissors' and player2 == 'rock'):
print 'Player 2 wins.'
if (player1 == 'paper' and player2 == 'scissors'):
print 'Player 2 wins.'
else:
print "This is not a valid object selection."
上面是代码,出于某种原因,当你输入的东西不是摇滚,纸张或剪刀时,else语句永远不会打印出来。有人可以帮我这个吗?
答案 0 :(得分:0)
条件!=
需要更改为==
player1 = raw_input("Player 1? ")
player2 = raw_input("Player 2? ")
if ((player1 == "rock" or
player1 == "paper" or
player1 == "scissors") and
(player2 == "rock" or
player2 == "paper" or
player2 == "scissors")):
if (player1 == 'rock' and player2 == 'rock'):
print 'Tie.'
if (player1 == 'paper' and player2 == 'paper'):
print 'Tie.'
if (player1 == 'scissors' and player2 == 'scissors'):
print 'Tie.'
if (player1 == 'paper' and player2 == 'rock'):
print 'Player 1 wins.'
if (player1 == 'rock' and player2 == 'scissors'):
print 'Player 1 wins.'
if (player1 == 'scissors' and player2 == 'paper'):
print 'Player 1 wins.'
if (player1 == 'rock' and player2 == 'paper'):
print 'Player 2 wins.'
if (player1 == 'scissors' and player2 == 'rock'):
print 'Player 2 wins.'
if (player1 == 'paper' and player2 == 'scissors'):
print 'Player 2 wins.'
else:
print "This is not a valid object selection."
答案 1 :(得分:0)
您的第一个myRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false){
@Override
public boolean canScrollHorizontally() {
return false;
}
@Override
public boolean canScrollVertically() {
return false;
}
});
语句将始终评估为if
。将true
替换为!=
,您会很高兴。
答案 2 :(得分:0)
您可以使用in
。
player1 = raw_input("Player 1? ")
player2 = raw_input("Player 2? ")
valid_opts = {'rock', 'paper', 'scissors'}
if player1.lower() in valid_opts and player2.lower() in valid_opts:
...
else:
print("invalid input")
答案 3 :(得分:0)
正如其他人所指出的,您需要将多个!=
更改为==
。您可能还会考虑一些缩短:
if len(set(player1, player2, "rock", "paper", "scissors")) == 3:
if player1 == player2:
print 'Tie.'
answers = ["Player 1 wins.", "Player 2 wins."]
elif player1 == "rock":
print answers[player2=="paper"]
elif player1 == "paper":
print answers[player2=="scissors"]
elif player1 == "scissors":
print answers[player2=="rock"]
else:
print "This is not a valid object selection."
答案 4 :(得分:0)
您可以使用dict
和一系列if/elif/else
语句简化整个程序:
priority = dict(rock='scissors', paper='rock', scissors='paper')
player_1 = raw_input('Player 1? ')
player_2 = raw_input('Player 2? ')
if player_1 not in priority or player_2 not in priority:
print 'This is not a valid object selection.'
elif player_1 == player_2:
print 'Tie.'
elif priority[player_1] == player_2:
print 'Player 1 wins.'
else:
print 'Player 2 wins.'
只需一点点工作,如果您愿意,可以让代码适应RPSSL的规则:
priority = dict(scissors={'paper', 'lizard'},
paper={'rock', 'spock'},
rock={'lizard', 'scissors'},
lizard={'spock', 'paper'},
spock={'scissors', 'rock'})
player_1 = raw_input('Player 1? ')
player_2 = raw_input('Player 2? ')
if player_1 not in priority or player_2 not in priority:
print 'This is not a valid object selection.'
elif player_1 == player_2:
print 'Tie.'
elif player_2 in priority[player_1]:
print 'Player 1 wins.'
else:
print 'Player 2 wins.'