为什么打印功能和分数不能继续工作?



我一直在为一个学校项目开发岩石、纸张、剪刀游戏,并开始编写记分代码
当我测试游戏时,一切都很好,除了上面写着谁赢了,并且没有为输赢或平局得分加一分的部分
我的主要问题是,它没有打印文本,说明谁赢了,什么打败了什么
我怎样才能使它工作?

这是我到目前为止的代码:

import random
win = 0
loss = 0
draw = 0
# first scores
while True:
name = input("Enter your name: ")
# asks user for name
if name.isalpha():
break
else:
print("Please enter characters A-Z only")
# makes sure user only enters letters
# taken from Stack Overflow
games = int(input(name + ', Enter in number of plays between 1 and 7: '))
# asks user how many games they want to play
if games >= 7:
print('1 to 7 only')
# makes sure user input doesn't exceed seven plays
while True:
user_play = input("Select r, p, or s: ")
# asks user to play rock, paper or scissors
if user_play.isalpha():
break
else:
print("Please enter characters A-Z only")
com_choice = ['r', 'p', 's']
print(random.choice(com_choice))
# Prints a random item from the list as the computer's choice
if user_play == com_choice:
print('Draw!')
draw = draw + 1
# if the user plays the same move as the computer, one point goes to draw
elif user_play == 'r' and com_choice == 'p':
print('Paper beats rock.')
print('AI wins!')
loss = loss + 1
# if the user plays rock and computer plays paper, says that the computer won and puts a point in the loss category
elif user_play == 'r' and com_choice == 's':
print('Rock beats scissors.')
print(name + ' wins!')
win = win + 1
# if the user plays rock and computer plays scissors, says that the person won and puts a point in the win category
elif user_play == 'p' and com_choice == 'r':
print('Paper beats rock.')
print(name + ' wins!')
win = win + 1
# if the user plays paper and computer plays rock, says that the person won and puts a point in the win category
elif user_play == 'p' and com_choice == 's':
print('Scissors beats paper.')
print('AI wins!')
loss = loss + 1
# if the user playspaper and scissors plays paper, says that the computer won and puts a point in the loss category
elif user_play == 's' and com_choice == 'r':
print('Rock beats scissors.')
print('AI wins!')
loss = loss + 1
# if the user plays scissors and computer plays rock, says that the computer won and puts a point in the loss category
elif user_play == 's' and com_choice == 'p':
print('Scissors beats paper.')
print(name + ' wins!')
win = win + 1
# if the user plays scissors and computer plays paper, says that the person won and puts a point in the win category

您的代码有3个问题,2个非常严重,1个非常小:

  • 它缺少游戏数量的循环
  • 它将字符串与列表进行比较,即:if user_play == com_choice:和所有其他比较
  • 它没有验证用户的选择,即:if user_play.isalpha():不作为'a''b'等输入
import random
win = 0
loss = 0
draw = 0
# first scores
while True:
name = input("Enter your name: ")
# asks user for name
if name.isalpha():
break
else:
print("Please enter characters A-Z only")
# makes sure user only enters letters
# taken from Stack Overflow
games = int(input(name + ', Enter in number of plays between 1 and 7: '))
# asks user how many games they want to play
if games >= 7:
print('1 to 7 only')
# makes sure user input doesn't exceed seven plays
choices = ['r', 'p', 's'] # the possible choices
for i in range(games): # the loop is needed to play multiple games
print(f"Match {i+1}, trust your instinct")
while True:
user_play = input("Select r, p, or s: ")
# asks user to play rock, paper or scissors
if user_play in choices:              # checks user input
break                             # if valid breaks the loop
print("Please enter only r, p, or s") # otherwise asks again
com_choice = random.choice(choices) # assigns the computer choice
print(com_choice)
# Prints a random item from the list as the computer's choice

if user_play == com_choice: # now the comparisons are correct
print('Draw!')
draw = draw + 1
# if the user plays the same move as the computer, one point goes to draw

elif user_play == 'r' and com_choice == 'p':
print('Paper beats rock.')
print('AI wins!')
loss = loss + 1
# if the user plays rock and computer plays paper, says that the computer won and puts a point in the loss category

elif user_play == 'r' and com_choice == 's':
print('Rock beats scissors.')
print(name + ' wins!')
win = win + 1
# if the user plays rock and computer plays scissors, says that the person won and puts a point in the win category

elif user_play == 'p' and com_choice == 'r':
print('Paper beats rock.')
print(name + ' wins!')
win = win + 1
# if the user plays paper and computer plays rock, says that the person won and puts a point in the win category

elif user_play == 'p' and com_choice == 's':
print('Scissors beats paper.')
print('AI wins!')
loss = loss + 1
# if the user playspaper and scissors plays paper, says that the computer won and puts a point in the loss category

elif user_play == 's' and com_choice == 'r':
print('Rock beats scissors.')
print('AI wins!')
loss = loss + 1
# if the user plays scissors and computer plays rock, says that the computer won and puts a point in the loss category

elif user_play == 's' and com_choice == 'p':
print('Scissors beats paper.')
print(name + ' wins!')
win = win + 1
# if the user plays scissors and computer plays paper, says that the person won and puts a point in the win category

现在你必须检查分数才能知道获胜者

最新更新