【问题标题】:RPS code not runs but will not return any outputRPS 代码不运行但不会返回任何输出
【发布时间】:2018-03-02 17:33:52
【问题描述】:

我正在尝试使用 Python 3.6.3 在 PyCharm 中创建一个石头、纸、剪刀游戏,但它在执行时不会返回任何输出,而且我的 IDE 中没有收到任何代码错误。

import random

class RPS:
    rock = 1        
    paper = 2
    scissors = 3

    def __init__(self, choice):
        self.choice = choice

    def play(self):
        print("Enter: \n1 for Rock, \n2 for Paper, \nor 3 for Scissors")
        choice = int(input("Enter in a number between 1 and 3: "))
        while (choice != 1 or choice != 2 or choice != 3):
            print("You have selected an invalid choice!")
            print("Enter: \n1 for Rock, \n2 for Paper, \nor 3 for Scissors")
            choice = int(input("Enter in a number between 1 and 3: "))
            print("You have selected", choice)

        computer = random.randint(1, 3)

        if computer == 1:
            print("The computer chose rock")

        if computer == 2:
            print("The computer chose paper")

        if computer == 3:
            print("The computer chose scissors")

        if choice > computer:
            print("You win!")

        elif choice < computer:
            print("You lose!")

        elif choice == computer:
            print("It is a tie!")

        play_again = input('Do you want to play again[yes/no]? ')
        if play_again.lower() == 'yes':
            self.play()

        if play_again.lower() == 'no':
            print("Thanks for playing! \nBYE!")

【问题讨论】:

  • 这个文件导入一个模块并定义一个类;你从来没有做过任何事情来导致你的任何代码被执行。你需要创建你的类的一个实例,并调用它的.play() 方法。

标签: python-3.x loops


【解决方案1】:

正如 jasonharper 所说,您没有实例化该类并运行它的 play 方法。

为此,添加到文件的底部:

game = RPS()
game.play()

还有一些其他问题,但我会让你自己尝试解决这些问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-26
    相关资源
    最近更新 更多