Python 为什么赢了';打印功能和记分功能是否正常工作?
我一直在为一个学校项目制作一个石头、布、剪刀游戏,并开始编写记分代码。Python 为什么赢了';打印功能和记分功能是否正常工作?,python,Python,我一直在为一个学校项目制作一个石头、布、剪刀游戏,并开始编写记分代码。 当我测试游戏时,除了上面写着谁赢的部分之外,一切都很好,并且没有给赢、输或平局分数加一分。 我的主要问题是,它没有打印文字说明谁赢了,什么打败了什么。 我怎样才能做到这一点 以下是我目前的代码: import random win = 0 loss = 0 draw = 0 # first scores while True: name = input("Enter your name: "
当我测试游戏时,除了上面写着谁赢的部分之外,一切都很好,并且没有给赢、输或平局分数加一分。
我的主要问题是,它没有打印文字说明谁赢了,什么打败了什么。
我怎样才能做到这一点 以下是我目前的代码:
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\u play==com\u choice:
- 它没有验证用户选择,即:
不作为如果用户播放。isalpha():
,'a'
等输入'b'
现在你必须检查分数才能知道获胜者它只运行一次,并且从不使用分数做任何事情。问题是你的剧本只玩了一轮游戏。每次运行脚本时,它都会再次从0开始。您需要一个循环来运行多轮。
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