0

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
Gama11
  • 31,714
  • 9
  • 78
  • 100
  • 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= ")` – Joe Ferndz Nov 26 '20 at 04:36
  • Anand, it may have to do with your vscode config. See if this link helps you get a resolution. https://stackoverflow.com/questions/53910646/input-functionality-is-not-working-with-python-in-vscode – Joe Ferndz Nov 26 '20 at 04:54
  • why is Python trying to `eval` your input string, if I run your code in VSC I can't reproduce the error – rioV8 Nov 26 '20 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? – Terry Jan Reedy Nov 26 '20 at 23:52

3 Answers3

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()
0

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

  • 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. – Joe Ferndz Nov 26 '20 at 04:52
  • yeah i did it by writing the variables first but it is not working im receiving same error. – Anand Sharma Nov 26 '20 at 08:43
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

Jill Cheng
  • 9,179
  • 1
  • 20
  • 25