Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python use raw_input with a variable

Tags:

python

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

like image 861
Favolas Avatar asked Dec 04 '10 23:12

Favolas


People also ask

What is the difference between input () and raw_input () in Python?

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.

Why is raw_input not defined in Python?

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.

How do you input a raw string in Python?

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.

Which function takes the raw observations as input?

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.


1 Answers

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.

like image 113
D.C. Avatar answered Sep 28 '22 10:09

D.C.