Is it possible to use raw_input with a variable?
For example.
max = 100
value = raw_input('Please enter a value between 10 and' max 'for percentage')
Thanks,
Favolas
There are two functions that can be used to read data or input from the user in python: raw_input() and input(). The results can be stored into a variable. raw_input() – It reads the input or command and returns a string. input() – Reads the input and returns a python type like list, tuple, int, etc.
The NameError: name 'raw_input' is not defined occurs when you try to call the raw_input() function using Python major version 3. You can only use raw_input() in Python 2. To solve this error, replace all instances of raw_input() with the input() function in your program.
Python raw string is created by prefixing a string literal with 'r' or 'R'. Python raw string treats backslash (\) as a literal character. This is useful when we want to have a string that contains backslash and don't want it to be treated as an escape character.
The raw_input() function which is available in python 2 is used to take the input entered by the user. The raw_input() function explicitly converts the entered data into a string and returns the value.
You can pass anything that evaluates to a string as a parameter:
value = raw_input('Please enter a value between 10 and' + str(max) + 'for percentage')
use + to concatenate string objects. You also need to explicitely turn non-strings into string to concatenate them using the str() function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With