I am writing a simple input and I keep getting an error. Fir example if I type in 'Eagle' it get name error eagle is not defined. Why is this?
print("The new word?")
newword = input()
Use raw_input instead if you don't want to evaluate the expression supplied. By default python evaluates whatever you supply to input as python expression, raising the name error.
newword = raw_input('the new word')
Otherwise, if you are meant on using input, then you need to enclose your entry string in quotes. Then python would consider it a string eliminating the NameError. Supply 'Eagle' instead of Eagle. Moreover, its better to supply the prompt string in input parameters i.e.
newword = input('The new word')
#supply 'Eagle' (in quotes)
Related
So I am trying to write a program that writes a word, fills 20 spaces with markup(< or >), then writes that same word backwards. It also takes user input.
Can anyone tell me what im doing wrong
while test2 == 1 :
test = input("Enter a word to format. Entering QUIT will exit the program:")
if test == ("quit"):
print("You have quit the program.")
break
else:
print("{0:*<20}{1:*>20})".format(
"test")(["test"::-1])
Syntactically you have a " in the wrong place in your last print statement.
print("{0:*<20}{1:*>20}").format("test")(["test"::-1])
^
You also some other syntax errors with your format() arguments, this will work for the literal string "test" but you'll need to replace it with a variable to use it with actual user input:
print("{0:*<20}{1:*>20}").format("test","test"[::-1])
Your last line has errors, try the follwing to get 20 spaces between the words
In [1]: s = "test"
In [2]: print('{0:<20}{1}'.format(s,s[::-1]))
test tset
You can also use print('{0}{1}{2}'.format(s," "*20,s[::-1])) which is subjectively cleaner.
I'm new to programming and I need some help for a ai robot I just started on
Here is my code:
complements = "nice" and "happy" and "good" and "smart" and "wonderful"
var = "You are a "+ complements
input = raw_input
if var in input:
print "Thank you!"
else:
print "Wuhhhhh?"
If I type in something other than "nice" it goes to the else statement.
Or statements don't work
First, the and keyword does not do what you want. It is used as a binary comparison. Due to the inner workings of Python, your variable complements will receive the value "wonderful". You want to put these words in a list (see here). You will then be able to manipulate these words using concatenation as such (for example):
var = "You are a " + ", ".join(complements)
Furthermore, raw_input is a function. It must be called as such: raw_input(). Otherwise, you just create an alias of the function which you named input. You would still have to call it by appending () to it in order to receive the user input.
I also don't understand your if var in input: statement. var is a sentence you made, why would you search it in the user input? It would be clearer to do if raw_input() in complements, or something along the lines of it.
If you are beginning to learn Python, I would recommend you to use Python 3 instead of Python 2. raw_input() was renamed input() in Python 3.
UserInput = raw_input('Enter something: ')
print type(UserInput)
print (UserInput)
This is a very simple piece of code that is supposed to tell me what type the enter input is. e.g int or bool. For some reason It always come up as string. Lets say I enter "1" (no quotes) when I am prompted to "Enter Something". That should return as type int. The problem I think lies in the fact that UserInput is a string "raw_input('Enter something: ')". How do I fix my script to return the type the input that the user gave me? I am using python 2.7
The raw_input() function always returns a string, it is documented as such:
The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.
Emphasis mine.
Perhaps you were looking for input() instead? It evaluates the input given as a Python expression and is the equivalent of eval(raw_input()):
Equivalent to eval(raw_input(prompt)).
This function does not catch user errors. If the input is not
syntactically valid, a SyntaxError will be raised. Other exceptions
may be raised if there is an error during evaluation.
If you entered 1, it'd be interpreted as an integer literal and type(UserInput) would print <type 'int'>.
You are missing a bracket here in the second line. To solve the problem try this:
UserInput = raw_input('Enter something: ')
print(type(UserInput))
print(UserInput)
How do I create an "if" statement to make sure the input variable is a number and not a letter?
radius = input ('What is the radius of the circle? ')
#need if statement here following the input above in case user
#presses a wrong key
Thanks for your help.
Assuming you're using python2.x: I think a better way to do this is to get the input as raw_input. Then you know it's a string:
r = raw_input("enter radius:") #raw_input always returns a string
The python3.x equivalent of the above statement is:
r = input("enter radius:") #input on python3.x always returns a string
Now, construct a float from that (or try to):
try:
radius = float(r)
except ValueError:
print "bad input"
Some further notes on python version compatibility
In python2.x, input(...) is equivalent to eval(raw_input(...)) which means that you never know what you're going to get returned from it -- You could even get a SyntaxError raised inside input!
Warning about using input on python2.x
As a side note, My proposed procedure makes your program safe from all sorts of attacks. Consider how bad a day it would be if a user put in:
__import__('os').remove('some/important/file')
instead of a number when prompted! If you're evaling that previous statement by using input on python2.x, or by using eval explicitly, you've just had some/important/file deleted. Oops.
Try this:
if isinstance(radius, (int, float)):
#do stuff
else:
raise TypeError #or whatever you wanna do
I want to wrap the colour input in quotes within python:
def main():
colour = input("please enter a colour")
So if I enter red into the input box it automatically makes it "red"
I'm not sure how to do this, would it be something along the lines of:
def main():
colour = """ + input("please enter a colour") + """
Kind regards
The issue is that this doesn't pass syntactically, as Python thinks the second " is the end of the string (the syntax highlighting in your post shows how it's being interpreted). The nicest to read solution is to use single quotes for the string: '"'.
Alternatively, you can escape characters (if you wish to, for example, use both types of quote in a string) with a backslash: "\""
A nice way of doing this kind of insertion of a value, rather than many concatenations of strings, is to use str.format:
colour = '"{}"'.format(input("please enter a colour"))
This can do a lot of things, but here, we are simply using it to insert the value we pass in where we put {}. (Note that pre-2.7, you will need to give the number of the argument to insert e.g: {0} in this case. Past that version, if you don't give one, Python will just use the next value).
Do note that in Python 2.x, you will want raw_input() rather than input() as in 2.x, the latter evaluates the input as Python, which could lead to bad things. In 3.x, the behaviour was fixed so that input() behaves as raw_input() did in 2.x.