def rps_round(comp_move):
x=input('Enter R, P, or S:')
if comp_move=='R':
if x=='R':
print('Computer selects',comp_move)
print('Tie!')
return 'T'
elif x=='P':
print('Computer selects',comp_move)
print('Player wins!')
return 'P'
elif x=='S':
print('Computer selects',comp_move)
print('Computer wins!')
return 'C'
else:
print('Invalid Move')
return rps_round('R')
elif comp_move=='P':
if x=='R':
print('Computer selects',comp_move)
print('Computer wins!')
return 'C'
elif x=='P':
print('Computer selects',comp_move)
print('Tie!')
return 'T'
elif x=='S':
print('Computer selects',comp_move)
print('Player wins!')
return 'P'
else:
print('Invalid Move')
return rps_round('P')
elif comp_move=='S':
if x=='R':
print('Computer selects',comp_move)
print('Player wins!')
return 'P'
elif x=='P':
print('Computer selects',comp_move)
print('Computer wins!')
return 'C'
elif x=='S':
print('Computer selects',comp_move)
print('Tie!')
return 'T'
else:
print('Invalid Move')
return rps_round('S')
这是rps_round的代码
现在编写一个函数rps_game(),它不接受任何参数,并玩石头剪刀布,直到玩家连续赢了3轮。打成平局算作一轮,应该将你的“连胜”计数器重置为0,但无效移动不算作一轮。你的功能还应该记录玩家完成这项任务所需的局数。您必须调用问题C中的rps_round函数才能执行此操作。
计算机应按如下方式移动:
如果是第一轮,电脑就出石头R。
如果是第二轮,电脑就出布P。
如果是第三轮,电脑就出剪刀S。
重复这个模式,在第四轮中再次使用石头。
每次调用rps_round时,都需要将其作为参数传入
(这并不是一个特别好的石头剪纸策略,但目标是测量用户花了多长时间来选择图案)。
打印出每轮比赛的回合数和玩家在每轮比赛后连续获胜的次数。一旦用户对计算机连续赢了三轮,该函数应返回所玩的总回合数。
提示:您将需要变量来跟踪轮数和玩家连续赢了多少轮。
约束:必须从rps_游戏中调用rps_round函数才能玩每一轮:不要重做rps_游戏中问题C的任何工作。
例子如下
rps_game()
Enter R, P, or S: P
Computer selects R
Player wins!
Rounds: 1 - Consecutive Wins: 1
Enter R, P, or S: S
Computer selects P
Player wins!
Rounds: 2 - Consecutive Wins: 2
Enter R, P, or S: R
Computer selects S
Player wins!
Rounds: 3 - Consecutive Wins: 3
3
rps_game()
Enter R, P, or S: P
Computer selects R
Player wins!
Rounds: 1 - Consecutive Wins: 1
Enter R, P, or S: S
Computer selects P
Player wins!
Rounds: 2 - Consecutive Wins: 2
Enter R, P, or S: S
Computer selects S
Tie!
Rounds: 3 - Consecutive Wins: 0
Enter R, P, or S: S
Computer selects R
Computer wins!
Rounds: 4 - Consecutive Wins: 0
Enter R, P, or S: Paper
Invalid Move
Enter R, P, or S: B
Invalid Move
Enter R, P, or S: S
Computer selects P
Player wins!
Rounds: 5 - Consecutive Wins: 1
Enter R, P, or S: R
Computer selects S
Player wins!
Rounds: 6 - Consecutive Wins: 2
Enter R, P, or S: P
Computer selects R
Player wins!
Rounds: 7 - Consecutive Wins: 3
7
rps_game()
Enter R, P, or S: R
Computer selects R
Tie!
Rounds: 1 - Consecutive Wins: 0
Enter R, P, or S: R
Computer selects P
Computer wins!
Rounds: 2 - Consecutive Wins: 0
Enter R, P, or S: R
Computer selects S
Player wins!
Rounds: 3 - Consecutive Wins: 1
Enter R, P, or S: Rock
Invalid Move
Enter R, P, or S: P
Computer selects R
Player wins!
Rounds: 4 - Consecutive Wins: 2
Enter R, P, or S: These
Invalid Move
Enter R, P, or S: Don't
Invalid Move
Enter R, P, or S: Count
Invalid Move
Enter R, P, or S: S
Computer selects P
Player wins!
Rounds: 5 - Consecutive Wins: 3
5