首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python 3:我的类函数没有按照我想要的方式运行

Python 3:我的类函数没有按照我想要的方式运行
EN

Stack Overflow用户
提问于 2018-08-06 01:15:06
回答 1查看 58关注 0票数 -1

我是python编程的新手,即将完成我的第一个python小游戏。它基本上是一个python宠物,当用户输入一些命令时,您会得到一组响应。我把这个函数放到我的宠物类中,但是它不工作。它只执行前两行。给我提供了输入的选项,但没有其他。

代码语言:javascript
复制
import random
play = True

class Pypet:
    def __init__(self,name,photo,phrases,age,hungry,thirsty):
        self.name = name
        self.photo = photo
        self.phrases = phrases
        self.age = age
        self.hungry = hungry
        self.thirsty = thirsty

    def stats(self):
        print('Hey its me ' + self.name)
        if self.hungry:
            if self.thirsty:
                print(self.name + ' is hungry and thirsty!')
            else:
                print(self.name + ' is ')
        else:
            print('My stomach is going to explode *BURBS*')

    def run(self):
        while play:
            print('#################')
            user = input().lower
            if user == 'help':
                for command in commands:
                    print(command)
            elif user == commands[0]:
                print(random.choice(self.phrases))
            elif user == commands[1]:
                print('Omnomnom')
                self.hungry = False
            elif user == commands[2]:
                self.stats()
            elif user == commands[3]:
                if pypet.thirsty:
                    print('Luk luk luk thanks!')
                    self.thirsty = False
                else:
                    print('Im not thirsty!')
            elif user not in commands:
                print('Unknown command!')



commands = ['chat', 'feed', 'stats', 'give water', 'quit']

py1 = Pypet('Lexit','-_-',['Shaq','bored af','Maaaaan'], 15, True, True)
py2 = Pypet('BabyLexit',':))',['Daadddyy','wabadabadabdu','chabadabaduu'], 15, True, True)


py1.run()

当我运行这个命令时,我可以输入,但它总是返回未知的命令并继续循环。任何帮助都将不胜感激!

EN

回答 1

Stack Overflow用户

发布于 2018-08-06 01:16:38

您在此处缺少调用括号:

代码语言:javascript
复制
user = input().lower()   # <---- missing parenthesis

程序现在可以工作了:

代码语言:javascript
复制
import random

play = True

class Pypet:
    def __init__(self,name,photo,phrases,age,hungry,thirsty):
        self.name = name
        self.photo = photo
        self.phrases = phrases
        self.age = age
        self.hungry = hungry
        self.thirsty = thirsty

    def stats(self):
        print('Hey its me ' + self.name)
        if self.hungry:
            if self.thirsty:
                print(self.name + ' is hungry and thirsty!')
            else:
                print(self.name + ' is ')
        else:
            print('My stomach is going to explode *BURBS*')

    def run(self):
        while play:
            print('#################')
            user = input().lower()     # <---- added parenthesis
            if user == 'help':
                for command in commands:
                    print(command)
            elif user == commands[0]:
                print(random.choice(self.phrases))
            elif user == commands[1]:
                print('Omnomnom')
                self.hungry = False
            elif user == commands[2]:
                self.stats()
            elif user == commands[3]:
                if pypet.thirsty:
                    print('Luk luk luk thanks!')
                    self.thirsty = False
                else:
                    print('Im not thirsty!')
            elif user not in commands:
                print('Unknown command!')



commands = ['chat', 'feed', 'stats', 'give water', 'quit']

py1 = Pypet('Lexit','-_-',['Shaq','bored af','Maaaaan'], 15, True, True)
py2 = Pypet('BabyLexit',':))',['Daadddyy','wabadabadabdu','chabadabaduu'], 15, True, True)

py1.run()

输出:

如果提示上的输入是:help

代码语言:javascript
复制
#################
help
chat
feed
stats
give water
quit
#################
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51696558

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档