#python
#python
Вопрос:
Я создаю игру «Камень, ножницы, бумага» на Python. Однако есть одна большая проблема — она не выводит то, что выбрал компьютер, и очки, поэтому я не знаю, выигрываю ли я в этот момент или нет.
Я попытался напечатать «player_choice», «python_choice», «p_player_choice» и «p_python_choice». Она распечатала правильную переменную, но не точки или то, что выбрал компьютер.
Код приведен ниже:
import random
player_points = 0
player_points = 0
python_points = 0
p_player_choice = 0
p_python_choice = 0
loopnum = 3
for number in range(loopnum):
print("1 - Rock")
print("2 - Paper")
print("3 - Scissors")
print()
print("What do you choose?n")
player_choice = input()
python_choice = random.randint(1,3)
if (player_choice == 1):
p_player_choice = "rock"
if (player_choice == 2):
p_player_choice = "paper"
if (player_choice == 3):
p_player_choice = "scissors"
if (python_choice == 1):
p_python_choice = "rock"
if (python_choice == 2):
p_python_choice = "paper"
if (python_choice == 3):
p_python_choice = "scissors"
if (player_choice == python_choice):
print("The computer chose ",p_python_choice,"!",sep="")
player_points = 1
python_points = 1
print()
print("Player -",player_points)
print("Computer -",python_points)
if (player_choice == 1) and (python_choice == 2):
print("The computer chose ",p_python_choice,",so it gets a
point!",sep="")
python_points = 1
print()
print("Player -",player_points)
print("Computer -",python_points)
if (player_choice == 1) and (python_choice == 3):
print("The computer chose ",p_python_choice,",so you get a
point!",sep="")
player_points = 1
print()
print("Player -",player_points)
print("Computer -",python_points)
if (player_choice == 2) and (python_choice == 1):
print("The computer chose ",p_python_choice,",so you get a
point!",sep="")
player_points = 1
print()
print("Player -",player_points)
print("Computer -",python_points)
if (player_choice == 2) and (python_choice == 3):
print("The computer chose ",p_python_choice,",so it gets a
point!",sep="")
python_points = 1
print()
print("Player -",player_points)
print("Computer -",python_points)
if (player_choice == 3) and (python_choice == 1):
print("The computer chose ",p_python_choice,",so it gets a
point!",sep="")
python_points = 1
print()
print("Player -",player_points)
print("Computer -",python_points)
if (player_choice == 3) and (python_choice == 2):
print("The computer chose ",p_python_choice,",so you get a
point!",sep="")
player_points = 1
print()
print("Player -",player_points)
print("Computer -",python_points)
print()
if ((player_points == 3) and (python_points<3)):
print("You win!")
if ((python_points == 3) and (player_points<3)):
print("You lose!")
if ((python_points == 3) and (player_points == 3)):
print("You draw!")
else:
print("You both lose!")
Ожидаемый результат:
1 - Rock
2 - Paper
3 - Scissors
What do you choose?
>>>2
The computer chose rock, so you get a point!
Player - 2
Computer - 1
Фактический результат:
1 - Rock
2 - Paper
3 - Scissors
What do you choose?
>>>2
1 - Rock
2 - Paper
3 - Scissors
What do you choose?
Комментарии:
1. Я не знаю, является ли это частью вашей проблемы, но обратите внимание, что оператор increment-assign в python является
=
, а не=
. Что-то вродеmyvar = 1
рассматривается python какmyvar = 1
и всегда будет устанавливать значение, равное 1.
Ответ №1:
В этой строке
player_choice = input()
Ваш скрипт считывает введенные вами символы. Сценарий не знает, что эти символы должны быть числом от 1 до 3, но символы интерпретируются как a string
, то есть так, как вы бы написали "2"
в своем сценарии. Таким образом, ни одно из ваших if
утверждений не совпадает.
Вам нужно было бы преобразовать player_choice в число, например, написав
player_choice = int(input())
Это преобразует то, что считывается в командной строке, в целое число.