嗨,我正在练习python,正在做rps游戏。 这是代码:
document.addEventListener('dragover', function(e) { e.preventDefault() })
我仅在玩家获胜或平局时定义,否则,我没有另外声明。但是,由于某种原因,它总是会始终输出丢失的输出,而当它为CPU打印选择时,它会打印一个字符串,描述选择(纸剪刀...),但是对于玩家选择,它会打印数字。 因此,很高兴得到您的意见,我做错了什么,也很高兴获得一些技巧,您的想法和技巧将使我的代码更高效,更专业
答案 0 :(得分:1)
if playerChoice == 1 and compChoice == 3:
print("You Win - Rock crushes Scissors.")
if playerChoice == 2 and compChoice == 1:
print("You Win - Paper covers Rock.")
if playerChoice == 3 and compChoice == 2:
print("You Win - Scissors cut Paper.")
if compChoice == 1 and playerChoice == 3:
print("You Lose - Rock crushes Scissors.")
if compChoice == 2 and playerChoice == 1:
print("You Lose - Paper covers Rock.")
if compChoice == 3 and playerChoice == 2:
print("You Lose - Scissors cut Paper.")
答案 1 :(得分:1)
在您现有的代码中。 else 仅适用于最后的 if 语句。因此,如果玩家选择不是 Rock
并且 cpu 选择不是 Scissors
,则打印 else。
要解决这个问题,请使用 elif 来创建一个 if 语句链。然后 else 仅在不满足任何 if 条件时才适用,例如
if player_choice == cpu_choice:
print"\n Its a Tie!/n{} you!".format(name)
elif player_choice == Choices.paper and cpu_choice == Choices.rock:
print"\n Congratulations!\n{} you won!".format(name)
elif player_choice == Choices.scissors and cpu_choice == Choices.paper:
print"\n Congratulations!\n{} you won!".format(name)
elif player_choice == Choices.rock and cpu_choice == Choices.scissors:
print"\n Congratulations!\n{} you won!".format(name)
else:
print"\n Too Bad You Lost!".format(name)
您还需要将返回值从 raw_input
转换为 int。否则它是一个字符串,永远不会等于任何表示选择的整数值。所以改用 int(raw_input())
。
我看到您试图通过使用 int(player_choice)
来做到这一点。 int 构造函数不能“就地”工作。即它不会将变量本身转换为 int。它返回一个整数值,将 player_choice
作为字符串保留。您可以看到,当我们在对 input
的调用中包装 int
时,我们正在捕获返回值,因此 player_choice
是一个 int(假设您的输入可转换为 int)。
这是适用于 python 2 和 python 3 的。
import random
from sys import version_info
if version_info.major == 2:
try:
input = raw_input
except NameError:
pass
class Choices:
rock = "Rock"
paper = 'Paper'
scissors = 'Scissors'
def rps_game():
# Getting the name of the player.
name = input("Welcome to Rock Paper Scissors Game!\n Enter your name:")
# The Players Choice.
while True:
player_choice = int(input("\n1-Rock\n2-Paper\n3-Scissors\n{} choose a number:".format(name)))
if player_choice == 1:
player_choice = Choices.rock
elif player_choice == 2:
player_choice = Choices.paper
elif player_choice == 3:
player_choice = Choices.scissors
# Getting the cpu choice.
cpu_choice = random.randint(1, 3)
if cpu_choice == 1:
cpu_choice = Choices.rock
elif cpu_choice == 2:
cpu_choice = Choices.paper
elif cpu_choice == 3:
cpu_choice = Choices.scissors
if player_choice == cpu_choice:
print("\n Its a Tie!/n{} you!".format(name))
elif player_choice == Choices.paper and cpu_choice == Choices.rock:
print("\n Congratulations!\n{} you won!".format(name))
elif player_choice == Choices.scissors and cpu_choice == Choices.paper:
print("\n Congratulations!\n{} you won!".format(name))
elif player_choice == Choices.rock and cpu_choice == Choices.scissors:
print("\n Congratulations!\n{} you won!".format(name))
else:
print("\n Too Bad You Lost!".format(name))
print ("\nCPU Choice: {}\n Your Choice: {}".format(cpu_choice, player_choice))
play_again = input("Want to Play Again? y/n")
if play_again != 'y':
break
rps_game()
这里也尝试用更少的代码实现它。
from __future__ import print_function
import random
from sys import version_info
if version_info.major == 2:
input = raw_input
choices = ('Rock', 'Paper', 'Scissors')
winlogic = {
'Rock': 'Scissors',
'Paper': 'Rock',
'Scissors': 'Paper'
}
def calcwinner(choice1, choice2):
if choice1 == choice2: return 0
return 1 if winlogic[choice1] == choice2 else -1
name = input("Welcome to Rock Paper Scissors Game!\nEnter your name: ")
while True:
index = int(input("1-Rock\n2-Paper\n3-Scissors\n{} choose a number: ".format(name)))
try:
player_choice = choices[index - 1]
except IndexError:
print('please make a valid choice')
continue
cpu_choice = random.choice(choices)
winner = calcwinner(player_choice, cpu_choice)
print('You chose {} and computer chose {}. '.format(player_choice, cpu_choice), end='')
if winner == 0:
print('Tie')
elif winner < 0:
print('Cpu won')
else:
print('You won!')
play_again = input("Want to Play Again? y/n: ")
if play_again not in ('y', 'Y', 'yes'):
break
答案 2 :(得分:0)
player_choice = raw_input("\n1-Rock\n2-Paper\n3-Scissors\n{} choose a number:".format(name))
int(player_choice)
此代码无效。 player_choice
被设置为raw_input
中的字符串,但是int(player_choice)
并没有任何作用。它创建一个整数,然后将其发送到void中。相反,您需要像这样将其重新分配给player_choice
:
player_choice = int(player_choice)