How to Read a Value from User Input into a Variable

How do I read user input into a variable in Bash?

Use read -p:

# fullname="USER INPUT"
read -p "Enter fullname: " fullname
# user="USER INPUT"
read -p "Enter user: " user

If you like to get the user's confirmation:

read -p "Continue? (Y/N): " confirm && [[ $confirm == [yY] || $confirm == [yY][eE][sS] ]] || exit 1

You should also quote your variables to prevent pathname expansion and word splitting with spaces:

# passwd "$user"
# mkdir "$home"
# chown "$user:$group" "$home"

How do I read a value from user input into a variable

You want read:

echo Please enter your name:
read name
echo $name

See read(1) for more.

Taking user input as string and printing a variable Python

You can do this with a dictionary:

~/tests/py $ cat rice.py
food_list ={"Rice":"Rice is nice" }

print("What item would you like to see from the list")
answer = input(": ")
if answer in food_list.keys():
print(food_list[answer])
~/tests/py $ python rice.py
What item would you like to see from the list
: Rice
Rice is nice

How to I turn a user's input into individual variables?

You should use a dictionary instead of individual variables. For example

encode = {
"A" : 0,
"B" : 1
}

Then you can use a string joiner to translate the input

output = "".join(str(encode[c]) for c in my_input) 

Get user input and loop through a lot of variables in order to match that input to one of the variables

READER BEWARE: The OP has had artificial constraints placed on them by their instructor. The mechanism demonstrated here is not something I'd suggest be used in production code. There are better ways of getting similar functionality, namely using common Python data structures rather than individual global variables. The big downside of this method, in addition to it being a hack, is that even though setting the variable values can be done simply by an entered name, you would have to change the code to add new "places". In production code, your map and the individual places on it should be fed to the code as input data. Your code shouldn't have to change when your map or list of places changes.

Knowing just what you want, I took a look, and immediately found that there is indeed a way to set the value of a global variable given its name. That mechanism is the globals() call. This call returns a dictionary containing references to all the global variables. The keys are the variable names, and the values are tied to the actual values of the variables.

Here is the most basic example of this given the code in the question:

x = "x"
y = "y"
red = "\033[31m" # color code

place = input("Where do you want to go?: ")

g = globals()
g[place] = red + g[place]

print("x =", x)
print("y =", y)

Test run 1:

Sample Image

Test run 2:

Sample Image

Notice that the added color code makes the output red from the point where that variable's value is printed until the end of the input.

Here's a more complete example that uses both a color code and an "end color" code so that only the variable's printed value is colored red. In this example, you can keep inputting 1 of w, x, y or z, and then just press Return by itself to exit:

w = "w"
x = "x"
y = "y"
z = "z"

red = "\033[31m" # color code
end="\033[0m" # cancel color code

g = globals()

while True:
print("w =", w)
print("x =", x)
print("y =", y)
print("z =", z)

map = x + """ ------------- """ + y
print("map =", map)

place = input("Where do you want to go?: ")
if not place:
break
g[place] = red + g[place] + end

Test run:

Sample Image

Note here that each variable name that you enter leads to that variable's value being changed to display in red. This occurs because the value of the actual global variable is being modified and then the value of that variable is printed directly. This was the key bit of functionality you were asking for.

BEWARE: This mechanism is not something I'd suggest that you use in production code. There are better ways to do these things, namely using dictionaries or lists. A downside of this method is that even though setting the variable values can be done simply, you still have to change your code to add "places". In production code, your map and the individual places on it should be fed to the code as input data. Your code shouldn't have to change when your map or list of places changes.

I hope this gives you what you needed. Happy programming!

Storing user input in variables

You can create a class called Person, return object of this class filled with the values you collect from GetInformation method, and use it as the parameter of another method. Example of the class:

public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Birthdate { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string PostCode { get; set; }
public string Country { get; set; }
}

But I highly suggest you to read some C# tutorial first. This is really basic stuff.

How to save a user's input as a variable?

What I'm proposing is a slight change to your round 3 code.

user_location = input("where are you from?:")

if user_location == "Buenos Aires":
kill_em_all = input("should we kill em all?:")

if kill_em_all == "yes":
print("damn straight soldier")

This looks to follow your requirements above.

Note that if they don't say they are from Buenos Aires, the program will end.

Sample Image



Related Topics



Leave a reply



Submit