Well, in python I was planning to do something like this:
>> echo text
text
>>
But I just cant find a way to do it!
I know that for user input i will use input()
, but how do I get it to
echo the text specified?
Well, in python I was planning to do something like this:
>> echo text
text
>>
But I just cant find a way to do it!
I know that for user input i will use input()
, but how do I get it to
echo the text specified?
OK, here for you start:
from __future__ import print_function
try:
input, range = raw_input, xrange
except NameError:
pass
def quit_():
raise SystemExit
def plus(a, b):
return int(a)+int(b)
commands = {'echo':print, 'quit':quit_, '+':plus}
while True:
c = input('>>').split(None, 1)
if len(c) == 2:
c, args = c
args = args.split()
elif c:
c, args = c[0], []
else:
#empty line
continue
try:
_ = commands[c](*args)
if _ is not None:
print(_)
except KeyError:
print('%s is not recognized command' % c)
except TypeError:
print('%s given wrong parameters: %s!' % (c, ' '.join(args)))
except SystemExit:
print('Bye, bye')
break
except ValueError:
print('Problem with values of parameters for %s: %s' % (c, ' '.join(args)))
"""Output:
>>ehco hi!
ehco is not recognized command
>>echo Hi, newbie!
Hi, newbie!
>>+ 12
+ given wrong parameters: 12!
>>+ 1 2.3
Problem with values of parameters for +: 1 2.3
>>+ 1 1
2
>>quit
Bye, bye
>>>
"""
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.