我是python编程的新手,即将完成我的第一个python小游戏。它基本上是一个python宠物,当用户输入一些命令时,您会得到一组响应。我把这个函数放到我的宠物类中,但是它不工作。它只执行前两行。给我提供了输入的选项,但没有其他。
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()
当我运行这个命令时,我可以输入,但它总是返回未知的命令并继续循环。任何帮助都将不胜感激!
发布于 2018-08-06 01:16:38
您在此处缺少调用括号:
user = input().lower() # <---- missing parenthesis
程序现在可以工作了:
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
#################
help
chat
feed
stats
give water
quit
#################
https://stackoverflow.com/questions/51696558
复制相似问题