1

enter image description here i created this code in my vscode to run. But it was showing error so i thought the code might be wrong so i did many changes in it. But nothing worked. Then i interpreted the same code in my idle, BOOOM! it worked, i reinterpreted it many times and it worked fine. So i want to know why is it not working in vscode. Please help me!

def vowel(y,z):
   if y in z:
       print("It is a vowel")
   else:
       print("It is a consonant")
z=('a','e','i','o','u','A','E','I','O','U')        
y=str(input("Enter a character= "))
vowel(y,z)

The error im getting in vscode

PS C:\Users\ANAND> python -u "c:\Users\ANAND\Desktop\python codes\vowel.py"
Enter a character= i
Traceback (most recent call last):
  File "c:\Users\ANAND\Desktop\python codes\vowel.py", line 9, in <module>
    y=str(input("Enter a character= "))
  File "<string>", line 1, in <module>
NameError: name 'i' is not defined
4
  • input statement will always be a string. You dont have to convert it to a string before you store into y. Just give y=input("Enter a character= ") Commented Nov 26, 2020 at 4:36
  • Anand, it may have to do with your vscode config. See if this link helps you get a resolution. stackoverflow.com/questions/53910646/… Commented Nov 26, 2020 at 4:54
  • why is Python trying to eval your input string, if I run your code in VSC I can't reproduce the error Commented Nov 26, 2020 at 11:11
  • In python 2, input() is evaluated, so my guess is that your VSCode defaults to 2.7. What does 'import sys; print(sys.version)' print? Commented Nov 26, 2020 at 23:52

3 Answers 3

1

Try doing this:

def vowel():
   z=('a','e','i','o','u','A','E','I','O','U')   
   y=str(input("Enter a character= "))
   if y in z:
       print("It is a vowel")
   else:
       print("It is a consonant")

     
if __name__ == "__main__":
    vowel()
Sign up to request clarification or add additional context in comments.

3 Comments

While this is a good answer, it still does not solve OPs problem with input() using vscode
can u send me the image of that thing in vs code. Coz when I tried running that in vs code it was running successfully
suhail i tried running your code but it is also showing same error :(
0

I noticed from your screenshot that the terminal uses "Code", but the terminal is in "Powershell" when executing the code, and the path is not the path of the currently opened file.

Please use the shortcut key Ctrl+Shift+` to open a new VSCode terminal, and then execute the code.

enter image description here

In addition, it is recommended that you disable the extension "code-runner" and use the cmd terminal to execute the code. (Please use "terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\cmd.exe", in settings.json, then click the green button in the upper right corner to execute the code.)

enter image description here

Comments

0

EDIT: put the variables inside the def because it cant read anything outside of the def

2 Comments

I dont think it has anything to do with the function definition. This line of code y=str(input("Enter a character= ")) is not working on vscode while it works on idle.
yeah i did it by writing the variables first but it is not working im receiving same error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.