我已经为这个游戏编写了一个代码,并且我已经为它编写了范围,执行此代码 5 次,然后询问我们的决定,是否继续或不继续,但是当我在终端中运行它并写下除 s 之外的另一个单词时,p,r 这轮也算,游戏会进行 4 次。
from random import randint
game_choices = {
'r': "rock",
'p': "paper",
's': 'scissor'
}
player_score = 0
pc_score = 0
for i in range(5):
player = input('please enter your choose from s(scissor),r(rock),p(paper):')
if player not in ['s', 'r', 'p']:
print('not a valid input!')
print('your choice is ' + game_choices[player])
choices = ['s', 'r', 'p']
random_number = randint (0, 2)
pc = choices[random_number]
print('computer choice is ' + game_choices[pc])
if player == pc:
print('_-_-_-_')
print('equaled')
elif player == 's':
if pc == 'r':
print('_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_')
print("!computer win! score it's for computer!")
pc_score += 1
else:
print('_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_')
print("!you win! score it's for you!")
player_score += 1
elif player == 'r':
if pc == 'p':
print('_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_')
print("!computer win! score it's for computer!")
pc_score += 1
else:
print('_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_')
print("!you win! score it's for you!")
player_score += 1
elif player == 'p':
if pc == 's':
print('_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_')
print("!computer win! score it's for computer!")
pc_score += 1
else:
print('_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_')
print("!you win! score it's for you!")
player_score += 1
print('---------------------------------------')
print("your total score:" + str(player_score))
print('....................')
print('computer total score:' + str(pc_score))
print('---------------------------------------')
if player_score == pc_score:
print("no one win")
elif player_score > pc_score:
print("!congratulation! totally you are the winner")
else:
print("totally pc is the winner")
while True:
game = input('do you want to play again? (y/n):')
if game == 'y':
pass
else:
break
如果您能帮助我解决这个问题,我将不胜感激。
解决此问题的一种方法是将轮次计数包含在 while 循环内,并且仅在提供有效输入时递增轮次计数器。这是经过此调整的修改后的代码
from random import randint
game_choices = {
'r': "rock",
'p': "paper",
's': 'scissor'
}
player_score = 0
pc_score = 0
rounds_played = 0
total_rounds = 5
while rounds_played < total_rounds:
player = input('Please enter your choice from s (scissor), r (rock), p (paper): ')
if player not in ['s', 'r', 'p']:
print('Not a valid input!')
continue # Skip the rest of the loop and restart
print('Your choice is ' + game_choices[player])
choices = ['s', 'r', 'p']
random_number = randint(0, 2)
pc = choices[random_number]
print('Computer choice is ' + game_choices[pc])
if player == pc:
print('-------------------------')
print('Equal!')
elif player == 's':
if pc == 'r':
print('-------------------------')
print("! Computer wins! Score goes to computer!")
pc_score += 1
else:
print('-------------------------')
print("! You win! Score goes to you!")
player_score += 1
elif player == 'r':
if pc == 'p':
print('-------------------------')
print("! Computer wins! Score goes to computer!")
pc_score += 1
else:
print('-------------------------')
print("! You win! Score goes to you!")
player_score += 1
elif player == 'p':
if pc == 's':
print('-------------------------')
print("! Computer wins! Score goes to computer!")
pc_score += 1
else:
print('-------------------------')
print("! You win! Score goes to you!")
player_score += 1
rounds_played += 1 # Increment the round counter
print('---------------------------------------')
print("Your total score: " + str(player_score))
print('....................')
print('Computer total score: ' + str(pc_score))
print('---------------------------------------')
if player_score == pc_score:
print("No one wins!")
elif player_score > pc_score:
print("! Congratulations! You are the winner!")
else:
print("The computer is the winner!")
while True:
game = input('Do you want to play again? (y/n): ')
if game == 'y':
break # Break out of this loop to restart the game
elif game == 'n':
break # Break out of the outer loop to exit the game
else:
print('Invalid input! Please enter y (yes) or n (no).')
这样,如果玩家输入无效输入,它将再次提示他们输入有效输入,而不将该回合计入已玩的总回合。