【问题标题】:Python 2.7 Rock Paper Scissors Not working as expectedPython 2.7 Rock Paper Scissors 未按预期工作
【发布时间】: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


【解决方案1】:
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)

【讨论】:

  • thnx 但这不是主要问题,主要问题是即使玩家输了或平局,程序也会运行赢家打印或平局打印以及输掉打印
【解决方案2】:
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.")

【讨论】:

  • 我个人会使用它,定义用户和计算机的结果。
【解决方案3】:

在您现有的代码中。 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 作为一个字符串。您可以看到,当我们在对 int 的调用中包装 input 时,我们正在捕获返回值,因此 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2015-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    相关资源
    最近更新 更多