1
name = input("What is your name?")
if input == name:
    print(final % (name, fav_color, weight, height))

I am creating a database-program that will spit out your favorite color, name, weight, and height. The problem being is that if I want to make lots of slots available, I need to be able to use variables (f.e: name, name1, name2).

However, I can't figure out how to use a variable from earlier in an if input == statement. I was trying to make it so that if you input the name (lets say it is Frank) I wouldn't really want to have to open the program in IDLE and have to edit the entire thing again just to add in Frank. So how do I make it so that I can make Frank into a variable, and use that as if it was like if input == name: print("final %(name, fav_color, weight, height) How can I do this?

vesche
  • 1,842
  • 1
  • 13
  • 19
nexy
  • 11
  • 1
  • Are you making a database using Python or are you using a database like MySQL, SQLite, SQL Server etc.? Do you ask the user for name, fav_color, weight, height etc. in this program or is it already stored in a database you access with Python? – zedfoxus Jan 02 '16 at 03:34
  • I am creating a database of anybody in my family. I am adding them as I move on, but this problem stumped me. – nexy Jan 02 '16 at 03:51

2 Answers2

0

I was trying to make it so that if you input the name (lets say it is Frank) I wouldn't really want to have to open the program in IDLE and have to edit the entire thing again just to add in Frank.

A lot of people use some sort of configuration file, typically as ~/.namerc (or ./.namerc) to store your preferences.

Reading a JSON File

If you want an example of getting a json file from a user's current directory, see this answer.

You'll need a json file, you'll want to include an area for what username to search for.

test.json

{
    "name": "Frank"
}

yourDatabaseScript.py

import json

with open("test.json") as json_file:
    json_data = json.load(json_file)
    print(json_data["name"])

On the command line

$> python ./yourDatabaseScript.py
Frank

Many times it's used to minimize the number of command line arguments, which are another popular way of getting input from a user without using input.

Reading from the Command Line

I prefer the vanilla python docs when reading up on it.

yourDatabaseScript.py

import argparse

parser = argparse.ArgumentParser(description='Get a name for a search.')
parser.add_argument('--name', dest='input', action='store_const',
                   const=str,
                   help='Enter a user\'s name to search')

args = parser.parse_args()
print args.input #  Frank

On the command line

$> python ./yourDatabaseScript.py --name="Not Frank"
Not Frank
$> python ./yourDatabaseScript.py --name Frank
Frank

On a side note

name = input("What is your name?")
if input == name:
print(final % (name, fav_color, weight, height))

input is a function that you've called on line one. You are checking that the value of calling input is the same thing as itself. I think this is a typo.

Community
  • 1
  • 1
yurisich
  • 6,991
  • 7
  • 42
  • 63
0

From what I understand you would like to create a database with all the info on family members.

So, if you have all the family members always predefined (i.e. you always know it will consist of Frank and who ever else) you can just set up a bunch of variables like name1 = Frank, name2 = Bob and so on, and then compare them with an if and elif statement:

if name == name1:
    doStuff()
if name == name 2:
    doStuff()
...

However this would be impractical over time if you would like to add to the database more people. What you can do is make a list/array.

family = []

And here you can put all the current family members.

family = ["Frank","Bob","Mary"]

You can then do the if statement in a for loop.

for element in family:
    if name == element:
        doStuff()

So what it's doing here is going through each element in the list, and comparing it to the name that has been imputed.

Lastly, if you would like to add to the current list of members, you can add a line of code that does this using the .append function in python.

family.append(name)

This will 'append' (add) it to the list (w/ out opening the source code of course). You could make a loop that checks if it's already in the list or whatever but that's up to you.

Hope this helps!

TMJ
  • 21
  • 5