1

I want my program to print the value of the variable 'sob' when I write: write("sob")

Code:

str = input(">")
sob = input(".")
if str == ('write("' + sob + '")'):
    print(sob)
else:
    print("SYNTAX ERROR")
MLCLOUD
  • 11
  • 8

2 Answers2

0

Instead of using input() use raw_intput():

str = raw_input('>')
sob = raw_input(".")

if str == ('write("' + sob + '")'):
        print(sob)
else:
        print("SYNTAX ERROR")

input() interprets the input you enter as python commands, whereas raw_input() returns a string.

user3814613
  • 249
  • 1
  • 2
  • 11
0

Try this

CODE

sob = raw_input('Type write("sob"): ')
sob = sob.lower()
if sob == 'write("sob")':
    print(sob.lstrip('Type write("').rstrip('")'))
else:
    print("Error")

RESULT

sob
  • I only want it to display the word sob – MLCLOUD Jul 01 '16 at 02:14
  • do you want the user to input 'write' or just 'sob' –  Jul 01 '16 at 02:15
  • I want the user to input the whole thing but it only prints sob – MLCLOUD Jul 01 '16 at 02:16
  • @MLCLOUD updated again to make it safe against differences in capitalization. The second line casts the input as all lowercase before the comparison is made. –  Jul 01 '16 at 02:32
  • Thanks! I will test to see if it works, does it work with python 3.x? – MLCLOUD Jul 01 '16 at 02:33
  • @MLCLOUD for 3.x just change 'raw_input' to 'input'. Raw_Input no longer exists in 3.x –  Jul 01 '16 at 02:36
  • Oh yes i already knew that. Im actually building a mini programming language in python - zircon-lang.weebly.com – MLCLOUD Jul 01 '16 at 02:38
  • Thanks. Do u know how to make a loop so the user can write and print as much as they want. – MLCLOUD Jul 01 '16 at 02:49
  • Yes, this can be done. I just logged off for tonight. I'll post a solution tomorrow afternoon –  Jul 01 '16 at 02:53
  • @MLCLOUD I asked a question myself that was similar. You should be able to modify this to fit your needs. http://stackoverflow.com/questions/37841115/python-input-prompt-go-back –  Jul 02 '16 at 12:56
  • Thanks a lot mate! – MLCLOUD Jul 03 '16 at 12:46