I am having problems with a simple program I wrote but do not know where the problem is, and it is giving me a Syntax error.
This is my code:
username = {}
temp = True
while temp:
name = input("Please input your username: ")
response = input("What is the place you want to visit? ")
username[name] = response
end = input("Do you want to end the program? Yes/No ")
if end == 'Yes':
temp = False
print("These are the results of the poll: ")
for names, responses in username.items():
print(names + " wants to go to " + responses)
This is my error:
File "<stdin>", line 1
/usr/local/bin/python3 "/Users/eric/Python Notes/Notes.py"
^
SyntaxError: invalid syntax
Check out the accepted answer here:
syntax error when using command line in python
Looks like your problem is that you are trying to run python test.py from within the Python interpreter, which is why you're seeing that traceback.
Make sure you're out of the interpreter, then run the python test.py command from bash or command prompt or whatever.
There are also some VSCode-specific tips here:
Invalid Syntax error when running python from inside Visual Studio Code
Related
When I'm running the following code:
print ("""Russian Lounge Pls Select Your Lang. etc(tr,en,ru)""")
lang = input(print "Please Select Your Lang :")
is generated a error. How I can solve it ?
BurNet#Casper MINGW64 ~/Desktop/RUlounge
$ python client.py
File "client.py", line 28
'''
^
SyntaxError: invalid syntax
If you want to show a message before the input prompt, just pass the string to input(), no need to use print there.
print("Russian Lounge Pls Select Your Lang. etc(tr,en,ru)")
lang = input("Please Select Your Lang: ")
print(lang)
Note: this assumes you are using Python 3.x, where the print statement uses parenthesis.
I tried to write a program which requests user to choose an option, however, Python always show an error message if user choose nothing and only input ENTER. Here is an example
tmp=input("Choose program type:1.C++;2.Python;3.PERL (ENTER for default 2.Python)")
print tmp, type(tmp) #test input
if len(str(tmp)) == 0:
tmp=0
if tmp == 1:
print "User choose to create a C++ program.\n"
DFT_TYPE=".cpp"
elif tmp ==2:
print "User choose to create a Python program.\n"
DFT_TYPE=".py"
elif tmp ==3:
print "User choose to create a PERL scripts.\n"
DFT_TYPE=".pl"
else:
print "User choose incorrectly. Default Python program would be created.\n"
DFT_TYPE=".py"
if I input ENTER only, I got error message like below
Traceback (most recent call last): File "./wcpp.py", line 17, in <module>
tmp=input() File "<string>", line 0
^ SyntaxError: unexpected EOF while parsing
How to handle such case if user input nothing? Any further suggestion would be appreciated.
Since you are using python 2, use raw_input() instead of input().
tmp=raw_input("Choose program type:1.C++;2.Python;3.PERL (ENTER for default 2.Python)")
...
...
if tmp!='':
tmp = int(tmp)
pass #do your stuff here
else:
pass #no user input, user pressed enter without any input.
The reason you are getting error is because in python2 input() tries to run the input statement as a Python expression.
So, when user gives no input, it fails.
you can use raw_input with a default value
x = raw_input() or 'default_value'
This question already has answers here:
input() error - NameError: name '...' is not defined
(15 answers)
Closed 6 years ago.
upon looking around a bit I have still not come to the conclusion as to why the following piece of python works in IDLE but not terminal. Any help would be appreciated.
print("Hello User!")
request_list = ['']
while True:
greeting = input('')
if greeting.lower() == "hello":
print("Who is this?")
print("Welcome back " + input() +", what can I do for you?")
break
elif greeting.lower() != "hello":
print("Show some manners!")
The error
Traceback (most recent call last):
File "courtney.py", line 23, in <module>
greeting = input('')
File "<string>", line 1, in <module>
NameError: name 'hello' is not defined
you are running python3 IDLE and the terminal is set to python2.
In your computer's environmental variables, you want to change the path to the location of your Python3 installation instead of the python 2.
Take a look at the picture, the one you want to change is PATH
If you dont want to change your environmental variables so that your terminal stays using python2, then you have to change your input and print statements.
the code below is the implementation of your code in python 2.7:
print "Hello User!"
request_list = ['']
while True:
greeting = raw_input("What is your name? ")
if greeting.lower() == "hello":
print "Who is this?"
print "Welcome back " + greeting +", what can I do for you?"
break
elif greeting.lower() != "hello":
print "Show some manners!"
The problem is that you use python 2.x in your terminal. If you have installed both you should be able to use the command 'python3' to run your code instead of the command 'python'.
In python 3 'input' can take an integer or a string. In python2 'input' cannot take a string. Only other stuff. In python 2.x you should use 'raw_input' take a string.
I am coding a script interpreter. It should generate a Telnet session to send AT commands.
Here is the script which it generated:
telentHandle = None
if telentHandle == None:
import telnetlib
telentHandle = telnetlib.Telnet(10.49.188.187, 23456)
telentHandle.read_until("login: ")
telentHandle.write(userName + "\n")
telentHandle.read_until("Password: ")
telentHandle.write(password + "\n")
telentHandle.write(AT + "\n")
When I run it, I get
File "H:/code/testgen/test_script.txt.py", line 10
telentHandle = telnetlib.Telnet(10.49.188.187, 23456)
^
SyntaxError: invalid syntax
What am I doing wrongly?
10.49.188.187 isn't a valid identifier in Python (or any language). You presumably need a string: "10.49.188.187".
I have tried many things to try to get text to speech to work in python 2.7 on a mac. I managed to write some simple codes using the system os such as:
from os import system
system('say Hello world')
This works alone:
from os import system
string2 = 'test'
string1 = 'hello world' + string2 + '.'
system("say %s" %(string1))
But if I do multiple say commands, like this:
system('say Please tell me your name.')
name = raw_input()
st = "Hello. Want pie" + name + "?"
system("say " + st)
I get this error after I enter my name:
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
Am I currently making a mistake in concept, or does having two say commands not work? If having two say commands do not work this way, then how can I use text to speech multiple times in python 2.7 with macintosh?
Trying changing the format and see if you have any better luck.
system('say Please tell me your name.')
name = raw_input()
st = "Hello. Want pie" + name + "?"
system('say %s' %(st))