When running python in netbeans, when I type in input, how and where do I take in input. eg
name = input("enter your name")
print("hello",name)
When I run this I get the output on the left in the shell but when I try and input anything in that window I get
Traceback (most recent call last):
File "C:\Users\Rahul\Documents\NetBeansProjects\NewPythonProject\src\newpythonproject.py", line 18, in <module>
name = input("enter your name")
File "<string>", line 1, in <module>
NameError: name 'hi' is not defined
I am using netbeans 8.2.
thanks!
When you use input() in Python 2, it performs and eval() on the user input.
To get the exact string the user typed, you have to use raw_input() to get exactly what the user typed.
Also in Python 2.7, the print is a statement rather than a function. You can lose the parentheses.
your code will look like,
name = raw_input("enter your name")
print "hello", name
If You use python version 2+
name = raw_input("enter your name")
print("hello {}".format(name))
If You use python version 3+
name = input("enter your name")
print("hello {}".format(name))
Related
Learning python as a beginner. I wanted to copy my code into CMD but it won't work. Here is code
calculation_to_units = 24
name_of_unit = "hours"
def days_to_units(number_of_days):
if number_of_days > 0:
return f"{number_of_days} days are {number_of_days * calculation_to_units} {name_of_unit}"
else:
return "Liczba dni musi być dodatnia :)"
user_input = input("Hello user, enter amount of days you want to calculate to hours\n")
user_input_number = int(user_input)
calculated_value = days_to_units(user_input_number)
print(calculated_value)
despite the fact that it works in Pycharm. I already checked paths. I am not able to solve this problem. When I type in python3 test.py it also says C:\Users\Borys\AppData\Local\Programs\Python\Python310\python.exe: can't open file 'C:\Users\Borys\test.py': [Errno 2] No such file or directory
Also recieved this message "unable to initialize device prn in python"
My internet connection is so bad that it took me 10 minutes to sign up on stack overflow. Additionaly my english knowledge is too small for complex programming explainations.
It can be difficult to paste code that calls input() into a python shell. Both the shell and the input() function read stdin. As soon as python reads the line with input(), it calls input() and that consumes the next line on stdin. In your case, that was a python code line intended to set a variable. That line was consumbed by input and was not read or executed by python. So you got a "variable not defined" error. But you would also have gotten another error because that line was also not the stuff you wanted to input.
Suppose you had the script
val = input("Input value: ")
i_val = int(val)
print(i_val)
And pasted it into the python shell
>>> val = input("Input value: ")
Input value: i_val = int(val)
>>> print(i_val)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'i_val' is not defined. Did you mean: 'eval'?
The line i_val = int(val) was assigned to val - it was not interpreted by the shell. There would be an ">>> " if it did.
I have a script for sending emails using SMTP_SSL. The code is working fine in PyCharm but in the terminal I get an error.
This is the code:
import smtplib
s = smtplib.SMTP_SSL("smtp.googlemail.com:465")
mml=input("enter your email address :\n")
str(mml)
passr=input("enter your pass:\n")
str(passr)
s.login(mml,passr)
em = input("please type the email you want to send :\n")
str(em)
a = input("please type the message:\n")
str(a)
s.sendmail(mml,em,a)
print("\nEmail Was Sent..:)")
When I run this in my terminal its giving this after i enter the email:
enter your email address :
mahmoud.wizzo#gmail.com
Traceback (most recent call last):
File "medo.py", line 3, in <module>
mml=input("enter your email address :\n")
File "<string>", line 1
mahmoud.wizzo#gmail.com
^
SyntaxError: invalid syntax
When I am trying to put the email between quotes, e.g. "mahmoud.wizzo#gmail.com" its working fine.
How can I run my script in the terminal?
I suspect you're running your script using Python 2 on the command line. The behaviour of input() changed in Python 3.
Try running python3 my_file.py instead of python my_file.py.
This is actually the way Python works. The problem you have is that input takes input from the user and then "executes" or "compiles" it so if you enter 4+9 in the input it will produce the number 13. Not the string "4+9". So try using raw_input() in Python 2. You can also use sys.stdin.readline() to get a string I'm any Python version. Don't forget import sys.
I have Spyder 2.3.8 that I installed with Anaconda.
Python version is 2.7.11, conda version is 4.0.5.
I have found that some types of code make the Spyder editor stop responding. One example is the line:
x = b'\x89\x50\x4E\x47\x0D\x0A\x1A\x0A'
I assume this is a valid python code. I am relatively new to Python, so please correct me if I am wrong here. I tried in on a terminal on the Python shell and it works.
Another very strange example is
png = 'oxy.png'
f = open(png, 'rb')
f.read(10) # That executes without any problems
f.close()
f = open(png, 'rb')
x = f.read(10) # this line makes Spyder freeze!
f.close()
The only difference here is that I assign f.read() to a variable, and that makes Spyder stop responding!
I just updated Spyder and all other anaconda packages. The error was happening before and it still happens. Any idea of what I could be doing wrong, or what could I try?
EDIT: There was this other part of the question which has been explained already (thanks!)
I also tried to run the following example I found on the internet:
name = input("What is your name? ")
print("Nice to meet you " + name + "!")
This example gives me an exception. Here is the full output:
name = input("What's your name? ")
print("Nice to meet you " + name + "!")
What is your name? sininho
Traceback (most recent call last):
File "<ipython-input-1-e82cc0e3f7a3>", line 1, in <module>
name = input("What's your name? ")
File "/scr/elbe9/pauline/physio/program/python_libraries/anaconda2/lib/python2.7/site-packages/ipykernel/ipkernel.py", line 165, in <lambda>
builtin_mod.input = lambda prompt='': eval(self.raw_input(prompt))
File "<string>", line 1, in <module>
NameError: name 'sininho' is not defined
In Python 2, the input() function executes the code you type in. To get q string, use raw_input() function which works the same way.
Every time I try and run my python code in the terminal I always get something like this,
Hello World
Enter your name: Tyler
Traceback (most recent call last):
File "HelloWorld.py", line 3, in <module>
name = input('Enter your name: ')
File "<string>", line 1, in <module>
NameError: name 'Tyler' is not defined
I am new to Python so please forgive me, I usually program in c# but Windows broke so I am trying to learn python.
Here is my code:
print('Hello World')
name = input('Enter your name: ')
print('Hi', name)
age = input('Enter your age: ')
age = int(age)
if (age == 35):
print('You are as old as Derek Banas')
if (age == 19):
print('You are the same age as me!')
else:
print('You are a different age than me')
print('Hello', name, 'You are', age, "It's nice to see you again!")
You should use raw_input instead of input.
You seem to be using python 3 so to run it from terminal you need to use python3 script.py instead of python script.py
Thank you all for being patient with me, here is the solution I have found based on everyones input! I was coding it in IDLE using Python 3.4 then just typing python in terminal. When I changed it to raw_input it fixed it but gave me an error running it in IDLE.
Here is the solution I have found:
When I use cd Desktop/Python then running it from there I should use Python3 not python so the code will look like this:
cd Desktop/Python
python3 HelloWorld.py
That fixed my issue! Thanks everyone who helped!
name = input('Enter your name: ')
age = input('Enter your age: ')
instead use
name = raw_input('Enter your name: ')
age = raw_input('Enter your age: ')
The only thing i have changed is downloaded pygame on my mac via the terminal.
When i type the follwing code -
name = input("what is your name?")
print ("Hello", name)
into my IDE i get this in the terminal -
what is your name? Traceback (most recent call last): File
"/Users/mikeyboy/PycharmProjects/untitled11/waht.py", line 1, in
name = input("what is your name?") File "", line 0
^ SyntaxError: unexpected EOF while parsing
Process finished with exit code 1
What's gone wrong this code worked perfectly a month ago?
I solved this by putting
name = raw_input("What is your name?")
instead of ...
name = input("What is your name?")
Anybody know why the latter worked before and now it doesn't?