For Python 3.x, use input(). For Python 2.x, use raw_input(). Don't forget you can add a prompt string in your input() call to create one less print statement. input("GUESS THAT NUMBER!").
Videos
I'm making an interactive menu and this problem apear and I don't know how to fix it, can soemeone please tell me how to fix it or tell me another approach for the error not to apear?
def funcion_uno():
print('Esta es la funcion uno')
def funcion_dos():
print('esta es la funcion dos')
if __name__ == '__main__':
salir = False
print('\033[1;32;40m Menu interactivo de Daft Punk')
mensaje = 'Ingrese la opcion deseada: '
menu = { 'a)': funcion_uno , 'b)': funcion_dos }
while not salir:
print('-'*len(mensaje))
print(mensaje)
for opcion, funcion in menu.iteritems():
print(opcion)
respuesta = raw_input('\nOpcion: ').lower()
salir = respueta == 'exit()'
else:
print('Hasta luego')
This is Python 2.7 and I'm trying to understand why one IDE throws an error and the other doesn't.
If I run the following code under Spyder:
name = raw_input("What is your name? ")
birthdate = raw_input("What is your birthdate? ")
age = input("How old are you?")
print("%s was born on %s" % (name, birthdate))
print("Half of your age is %s" (age /2 ))I then get
NameError: name 'raw_input' is not defined
If I run the same code under pythontutor.com, the code runs without error.
Just wondering why there is a difference between these IDEs and how to fix it under Spyder.
thanks
I have to edit this python file for one of my classes. I have the python extension by microsoft installed on vs code on mac os with the latest updates. When I try to run the program, I get this error: File "/Users/Downloads/decrypt.py", line 8, in <module>code = raw_input(jls_extract_var)NameError: name 'raw_input' is not defined
What could be causing this issue? It's a program already given to me by my professor that I just have to edit, but the raw input function isn't even letting me test the program.
This is the program that I need to edit, in case I may just be having a syntax or logical error.
"""File:Decypts an input string of lowercase letters and printsthe result. The other input is the distance value."""
code = raw_input("Enter the coded text: ")distance = input("Enter the distance value: ")plainText = ''for ch in code:ordValue = ord(ch)cipherValue = ordValue - distanceif cipherValue < ord('a'):cipherValue = ord('z') - (distance - \(ordValue - ord('a')) - 1)plainText += chr(cipherValue)print(plainText)
The difference is that raw_input() does not exist in Python 3.x, while input() does. Actually, the old raw_input() has been renamed to input(), and the old input() is gone, but can easily be simulated by using eval(input()). (Remember that eval() is evil. Try to use safer ways of parsing your input if possible.)
In Python 2, raw_input() returns a string, and input() tries to run the input as a Python expression.
Since getting a string was almost always what you wanted, Python 3 does that with input(). As Sven says, if you ever want the old behaviour, eval(input()) works.