1

I need a program that get a 1 line input from user(text) and then give the output as text(I write an example down below)

I tried for if but it's only accept one line code and if I write a word that is not defined, it ruin the rest.

class meaning(): def shortcutA (self): print ("ice cream") def shortcutB (self): print ("Choclet cake") def main(): m = meaning() if input() == "a": print("your code is: ") m.shortcutA() elif input() == "b": print("your code is: ") m.shortcutB() else : print ("unrecognized") print ("Please enter the words :") if __name__ == "__main__": main() 

I expect when I enter a b the result be like

ice cream Choclet cake 

Thank you.

1

3 Answers 3

1

We can use a for loop to go through the input in a word.

class meaning(): def shortcutA (self): print ("ice cream") def shortcutB (self): print ("Choclet cake") def main(): m = meaning() print_flag = False for i in input(): if i in ['a', 'b'] and not print_flag: print("your code is: ") print_flag = True if i == "a": m.shortcutA() elif i == "b": m.shortcutB() elif i == ' ': continue else : print ("unrecognized") print ("Please enter the words :") if __name__ == "__main__": main() 

produces:

Please enter the words : your code is: ice cream Choclet cake 
Sign up to request clarification or add additional context in comments.

Comments

0

You need to modify your if input statements. If you want output according to input separated by spaces, then use this:

for x in input(): if(x=='a'): print(m.shortcutA(),end=' ') if(x=='b'): print(m.shortcutB(),end=' ') else: print('unrecognised!') 

hope this helps..

Comments

0

I would suggest a program like this,

class meaning(): def shortcutA(self): return "ice cream" def shortcutB(self): return "Chocolet cake" def main(): m = meaning() code = '' for alphabet in user_input: if alphabet == "a": code += ' ' + m.shortcutA() elif alphabet == "b": code += ' ' + m.shortcutB() if len(code) == 0: print 'Unrecognized.' else: print 'The code is : ' + code user_input = raw_input('Please enter the words : \n') if __name__ == "__main__": main() 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.