【发布时间】:2018-08-05 18:14:17
【问题描述】:
嗨,我正在练习 python 和做 rps 游戏。 这是代码:
class Choices:
rock = "Rock"
paper = 'Paper'
scissors = 'Scissors'
def rps_game():
# Getting the name of the player.
name = raw_input("Welcome to Rock Paper Scissors Game!\n Enter your name:")
# The Players Choice.
while True:
player_choice = raw_input("\n1-Rock\n2-Paper\n3-Scissors\n{} choose a number:".format(name))
int(player_choice)
if player_choice == 1:
player_choice = Choices.rock
if player_choice == 2:
player_choice = Choices.paper
if 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
if cpu_choice == 2:
cpu_choice = Choices.paper
if cpu_choice == 3:
cpu_choice = Choices.scissors
if player_choice == cpu_choice:
print"\n Its a Tie!/n{} you!".format(name)
if player_choice == Choices.paper and cpu_choice == Choices.rock:
print"\n Congratulations!\n{} you won!".format(name)
if player_choice == Choices.scissors and cpu_choice == Choices.paper:
print"\n Congratulations!\n{} you won!".format(name)
if 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 = raw_input("Want to Play Again? y/n")
if play_again == 'y':
continue
else:
break
我只定义了当玩家获胜或平局时,否则我做了 else 声明。但是还有一个很大的,但是由于某种原因,它总是会输出丢失的输出,当它打印 CPU 的选择时,它会打印一个描述选择的字符串(Paper Scissors ...)但是对于玩家的选择,它会打印数字 所以我很高兴得到你的意见我做错了什么,也很高兴得到一些提示你的想法和提示是什么让我的代码更高效和专业
【问题讨论】:
-
raw_input()返回一个字符串,所以player_choice == 1永远不会为真。 -
更正拼写错误,更新标题
-
你忘了
elif
标签: python python-2.7