r/pythonhelp • u/[deleted] • Jan 19 '20
a function not working
I have a script, and I'm not sure why it isn't working:
def command(string):
commands = [
'wireshark',
'atom',
'gedit',
'chrome'
]
outputs = [
'sudo wireshark',
'atom',
'gedit',
'google-chrome-stable'
]
for i in range(len(commands)):
if (string == commands[i]):
return outputs[i]
break
elif not (string == commands[i]):
W = 1+1
else:
return "Error: Command \""+string+"\" not in Torrent commands list."
def main():
while True:
string = input('Torrent:~# ')
L = command(string)
if (L == string):
print('ya huh')
elif (string == 'exit'):
break
else:
print(command(string))
main()It opens up a prompt until you type in exit, which works fine. when you type in say "chrome", instead of saying 'ya huh', it says 'google-chrome-stable', and when you put in 'blob' for example, it says 'None' instead of the error text which would be "Error: Command "blob" not in Torrent commands list." why?
2
Upvotes
1
3
u/puplicy Jan 19 '20
Let me propose a version of the command() which looks more pythonic