我最近与小型个人助理项目合作,现在我正面临这个问题/错误,我无法克服它。
这是我的代码的一部分:
import os
import sys
from colorama import Fore, Back, Style
import random
from os import system
from src import commands
system("title Assistant")
actions = {
"open":["pubg", "dota", "origins", "spotify", "dogs"],#"open":{["o"]:["pubg", "dota", "origins", "spotify", "dogs"]},
"hue":["1"],
"clear":"",
"hiber":"",
"shutdown":""
}
class MainClass:
#
logo1 = Fore.CYAN + """Not essential"""
logo2 = Fore.CYAN + """Not essential"""
errorcode = "Something went wrong :("
def getCommand(self):
cmd = input(Fore.MAGENTA + "Assistant > " + Fore.CYAN)
print("cmd: " + cmd)
self.checkCommand(cmd)
def checkCommand(self, cmd):
actions = commands.Commands().actions
words = cmd.lower().split()
print("Words: " + ' '.join(words))
found = False
ekasana = ""
par = ""
print("running if " + words[0] + str(words[0] == "q"))
#Here's the problem. After I imput 'clear', which clear's the screen and runs mainInterface(2.0, randomthing), this if does not work.
# Here's the output
# Not essentialv 2.0
# By Dudecorn
# Assistant > q
# cmd: q
# Words: q
# running if qTrue
# self.errorcode
# clear
# ['clear']
# Assistant >
# Why is is that clear command staying there? I am so confused right now.
# Read line 68
if words[0] == "q":
quit()
sys.exit()
for word in words:
word = ''.join(word)# Sorry about the mess
print(word)
# Check for action without parameters
if word in actions and actions[word] == "" and found == False:
try: # I'm pretty sure that this part of code is causing the problem
# If you remove try and except, and leave just lines 70 and 71, code works as line 58 if statement's value is true.
# This is in the another file -> getattr(commands.Commands, word)(self)
self.mainInterface(2.0, random.randint(1, 2))
break
except:
print("self.errorcode")
print(word)
print(words)
# Check for action that has parameters
elif word in actions and not actions[word] == "" and found == False:
ekasana = word
found = True
# Check for parameters
elif not ekasana == "" and found == True:
for n in actions[ekasana]:
if n == word:
par = word
try:
getattr(commands.Commands, ekasana)(self, par)
except:
print(self.errorcode)
else:
print("Command not found")
self.getCommand()
def mainInterface(self, v, logo):
os.system('cls' if os.name == 'nt' else 'clear')
if logo == 1:
print(self.logo1+"v "+str(v)+"\n By Dudecorn")
else:
print(self.logo2+"v "+str(v)+"\n By Dudecorn")
self.getCommand()
这是主文件
import test
import random
def main():
m = test.MainClass()
m.mainInterface(2.0, random.randint(1,2))
main()
因此,当您运行代码并首先输入'clear'然后q时if语句将不会执行。我想知道为什么。我还注意到,如果你删除try和除了循环之后的第一个if语句,代码完美地工作。我可以删除它们,但它不会回答我的问题,为什么代码不能正常工作。另外,从文件中删除try和except不会对第一个if语句的执行方式产生任何影响,因为它会在代码中稍后出现。
抱歉英语不好,因为它不是我的主要语言,谢谢你的答案。我也想为代码中的那个巨大的混乱道歉。
答案 0 :(得分:0)
我不确定这是否是您正在寻找的答案,但检查可能会有用。
从下面的代码
def main():
m = test.MainClass()
m.mainInterface(2.0, random.randint(1,2))
main()
,我看到通过运行main()
,您还可以运行m.mainInterface
功能。
现在..你可能想检查一下:
方法mainInterface
最终会调用方法getCommand
,最终会调用checkCommand
,它将会遇到try
块在for word in actions
循环中,以及此try
块内...再次调用 mainInterface
..因此此过程将不断重复。