Python input method label gives error while printing it - python

I am practicing python on my own and struggling to under input method. My program is as below
a = (input('Enter any alphabet: '))
print 'type is: ',a
if a=='a':
print 'The given character is vowel a '
elif a=='e':
print 'The given character is vowel e'
elif a=='i':
print 'The given character is vowel i'
elif a=='o':
print 'The given character is vowel o'
elif a=='u':
print 'The given character is vowel u'
else:
print 'The give character is a consonent'
print "Thats all folks"
It is giving error when I am entering single digit alphabet as shown below
Enter any alphabet: a
Traceback (most recent call last):
File "Demo_if_ladder.py", line 1, in <module>
a = input('Enter any alphabet: ')
File "<string>", line 1, in <module>
NameError: name 'a' is not defined
Why it is happening, The program works fine if I input single digit alphabet as in single quote but it not accepting single digit alphabet without single quote

It looks like you're running python 2 from the print statement syntax.
In python 2, input(...) tries to run the contents like it was a command. So when you enter something in, python will try to execute it.
The solution to your problem is to use raw_input(...) instead which will return a string.

You are using Python 2.
In Python 2 input accepts Python commands. You can use raw_input to get the effect you desire.
The single quotes fix the issue because text in Python commands are written in single quotes.
You'll understand clearly if you run the following:
a = (input('please, write: "list(range(10))"\n'))
print a
a = raw_input('please, write: "list(range(10))"\n')
print a
In Python 3 input works as you expected, but the print statement was changed into a function. In Python 3 you should change
print a
to
print(a)

Related

NameError when assigning input() to variable (Python2.7.11) [duplicate]

This question already has an answer here:
Differences between `input` and `raw_input` [duplicate]
3 answers
I get a name error even though I'm just trying to put a string into a variable.
I am trying to do this on Python 2.7.11. Does anyone have anything that helps? Upgrading Python is not an option for me.
def translate(phrase):
translation = ""
for letter in phrase:
if letter in " ":
translation = translation + "#"
result = (input("enter a phrase you want encrypted: "))
result = translate(result)
This is the error that's shown:
Traceback (most recent call last):
File "D:\hello\encrydecry\encryption1.py", line 158, in <module
>
result = (input("enter a phrase you want encrypted: "))
File "<string>", line 1, in <module>
NameError: name 'hello' is not defined
In Python 2, when you use input(), Python interprets the input. So when you type hello, hello is interpreted as a variable, and you're essentially doing result = hello. Hence the error NameError: name 'hello' is not defined.
One option is to simply type the input between quotes, so it will be interpreted as a string: 'hello'.
To avoid the input being interpreted altogether, you have to use raw_input() instead of input(), which doesn't interpret the user input and always returns a string:
result = raw_input("enter a phrase you want encrypted: ")
def translate(phrase):
translation = ""
for letter in phrase:
if letter in " ":
translation = translation + "#"
result =raw_input("enter a phrase you want encrypted: ")
result = translate(result)

Removing the 'Return Space' inserted before input() - Python 3.6

Good day all,
Beginner's question here. In Python 3.6, I want to use the input() function, but when I do, it automatically inserts a return space between the prompt and the previous print output. Here's an example :
Code
print('a')
letter = input('letter: ')
print(letter)
Actual Output
a
letter: b
b
Desired Output
a
letter: b
b
Use the end= parameter. This will redefine what print appends. It looks like your string in question has a trailing newline in it and that isn't being reflected correctly in your editor.
print('a', end="")
letter = input('letter: ')
print(letter)

Python function returning different values depending on prior inputs

Learning python on Codeacademy.
This is a simple program for Pig Latin Translator.
As the title says, depending on prior inputs(which are intentionally entered incorrect), the function is returning different(first) value instead of latest value as cleared by if else. I tried to put return inside else but as seen in #Reference the local variable isn't returned. Please help.
def string_check():
name = raw_input("Enter a word")
if len(name) == 0 or name.isalpha() == False:
print "Enter a valid word"
string_check()
else:
print name
return name
print 'Welcome to the Pig Latin Translator!'
# Start coding here!
original = string_check()
print ("Original variable is " +original)
ans = original[1:len(original)] + original[0] + "ay"
print ("Pig Latin word is %s"% (ans))
# Reference
omega = 3
if omega == 3:
print "Obvious"
beta = 5
print beta
print beta
OUTPUT
1: When I entered a blank as first input
Welcome to the Pig Latin Translator!
Enter a word
Enter a valid word
Enter a word 123
Enter a valid word
Enter a word 123gas
Enter a valid word
Enter a word gas
gas
Original variable is
Traceback (most recent call last):
File "python", line 15, in <module>
IndexError: string index out of range
2: When I entered first input as 123
Welcome to the Pig Latin Translator!
Enter a word 123
Enter a valid word
Enter a word
Enter a valid word
Enter a word 123gas
Enter a valid word
Enter a word
Enter a valid word
Enter a word gas
gas
Original variable is 123
Pig Latin word is 231ay
Obvious
5
5
None
3. When I enter correct input directly
Welcome to the Pig Latin Translator!
Enter a word gas
gas
Original variable has gas
Pig Latin word is asgay
Obvious
5
5
None
Inside the string_check function you call itself on line 4. The returned result (name) is not captured however. One solution is to replace string_check() on line 4 with name = string_check().

How to do ghost-like typist in Python

I'm a beginner, how can I do ghost-like typist in console?
EDIT:
I did something like this, just prints a letter per line:
def ghostPrint(sentence):
for letter in sentence:
time.sleep(0.12)
print (letter)
ghostPrint("Hello world...")
This one, just changes letter in the same line:
def ghostPrint(sentence):
for letter in sentence:
time.sleep(0.12)
print (letter, end="\r")
ghostPrint("Hello world...")
And this one, prints Hello World... then closes:
def ghostPrint(sentence):
for letter in sentence:
time.sleep(0.12)
print (letter, end = " ")
ghostPrint("Hello world...")
I am currently using Python 3.5.
In Python 2, a trailing comma after a print statement will suppress the newline (it also adds a space between the arguments to print). So
print var1, var2,
prints the values of var1 and var2 with a space in between, and does not print a trailing newline.
In Python 3, the print function takes two arguments, sep and end. The default values are sep=' ' and end='\n'. Use sep to change what string is used to separate arguments to print, and use end to change what string is printed after everything else.
Use a trailing comma(,) to avoid a newline.
import time
def ghostPrint(sentence):
for letter in sentence:
time.sleep(0.12)
print (letter),
ghostPrint("Hello world...")
In python 3.x using print (letter,end='') will not work I guesses as it print every thing at once.

Need help writing a Python script that will input a sentence and count the number of words it contains

def main():
p =input("Enter your sentence: ")
words = p.split()
wordCount = len(words)
print ("The word count is:", wordCount)
main()
I get the read out:
Enter your sentence: Hello world
Traceback (most recent call last): File "C:/Python27/idk.py", line 11, in main() File "C:/Python27/idk.py", line 3, in main p =input("Enter your sentence: ") File "", line 1 Hello world ^ SyntaxError: unexpected EOF while parsing
What am i doing wrong? D:
See the documentation on input. The input function tries to evaluate the given input as a python command.
Try using raw_input instead, which returns the input as a string.
You tagged Python 2.7, so I am assuming you are using 2.7. If that is the case, you want to use raw_input() to take string input, not input(). In Python 3, raw_input is replaced by input() function, but in Python 2, the input() function takes input literally, not as a string.
def main():
p = raw_input("Enter your sentence: ") # raw_input() function
words = p.split()
wordCount = len(words)
print ("The word count is:", wordCount)
main()
Just an addition, if you wanted the input to be an integer, you can still use the raw_input() function. Just int() it! numinput = int(raw_input('Enter a number'))

Resources