Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have an original list of sites:
original_list = ["http://www.firstSite.com", "http://secondSite.com", "http://thirdSite.com", "http://www.fourthSite.com"]
I want to allow user to choose some sites and arrange the new array like this:
new_list = ["http://secondSite.com", "http://www.fourthSite.com"]
The filling of the new array depends on user's choice
You can try this. It takes the input and inserts it into a new array:
original_list = ["http://www.firstSite.com", "http://secondSite.com", "http://thirdSite.com", "http://www.fourthSite.com"]
new_array = input("Choose some sites from %s. Separate each website by a space " % (original_list)).split()
for site in new_array: #Checking if the sites entered by user are in the original_list
if site not in original_list:
print ("Error!")
You can also use the index of a site to ask the user for input. Like this:
original_list = ["http://www.firstSite.com", "http://secondSite.com", "http://thirdSite.com", "http://www.fourthSite.com"]
user_input = input("Enter the indices of the websites in %s you choose separated by a space " % (original_list)).split()
new_array = [original_list[x] for x in user_input]
for site in new_array: #Checking if the sites entered by user are in the original_list
if site not in original_list:
print ("Error!")
Both of these will create a list containing the sites that the user selected.
It's hard to give you a clear solution as you don't provide what you are trying to accomplish. So, let me know if this works for you and if it doesn't, I'm happy to delete my answer.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
First off, really sorry for the title. I have this list websites which contains more lists. In all of those sub-lists are different website's information. Example:
websites=[['Amazon','www.amazon.com'],['Stackoverflow','www.stackoverflow.com']]
I also have a separate program that allows the user to make an account (Username and a Password). For every user, I want them to have their own websites list with any websites they want to add. I know that probably isn't possible but I don't want to add a new websites variable for every user. The code for adding information about websites (Web name,URL,username,password,email):
def add_website():
clear_output(wait=False)
website=input("Website name: ")
clear_output(wait=False)
url=input("URL: ")
clear_output(wait=False)
username=input("Username: ")
clear_output(wait=False)
password=input("Password: ")
clear_output(wait=False)
email=input("Email: ")
clear_output(wait=False)
dic=[website,url,username,password,email]
websites.append(dic)
clear_output(wait=False)
I essentially want every username-password combination to have a separate websites variable. So that every account only has the websites they added. Is there a shorter way to do this rather than just creating a huge list with every user-password combination having a different list for their websites attached?
Create a class to represent a single user:
class MyUser():
def __init__(self, usr, pas):
self.usr = usr
self.pas = pas
def add_website(self, web_name, url):
self.web_name = web_name
self.url = url
So if you had a username "user_a" with a pass "pass_a" you could create obj like:
ua = MyUser("user_a", "pass_a")
And another "user_b", "pass_b":
ub = MyUser("user_b", "pass_b")
create a list of users and add in details about websites, assuming one website per user. If you want users to be able to have more than one website, consider a Sequence or Dict type for the attributes in MyUser.
result = [] # hold objs with websites added
for i, u in enumerate([ua, ub]):
u.add_website(*websites[i])
result.append(u)
Your output is a list result of users with distinct credentials and websites attached to each type MyUser.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a piece of code that is a storyline but I want to put it in a certain order.
Outside = ["You look around for something to help you", "You remember the toolbox in the back of the plane", "You go to the back of the plane, and open the stairwell", "You open the back and see the toolbox"]
for Outside in Outside:
print(Outside)
time.sleep(3)
#Outside------------------------------------------------------
Inside = ["you sit inside this plane all alone", "A gentle breeze rustles the wheat around you"]
for Inside in Inside:
print(Inside)
time.sleep(3)
#Inside-------------------------------------------------------
Landing = ["Landing gear out", "Touchdown"]
Exitplane = input("\nDo you want to exit the plane: ")
if Exitplane == ("y"):
print("You hop onto the ground" + Outside)
if Exitplane == ("n"):
print(Inside)
#Endcode-------------------------------
As you can see above that is the code I want to run but I want to run the Landing section first then run either Inside or outside (depending on user input)
Thank you for your time
Create a list with the str-values you have created:
story = [Outside, Inside, Landing]
Then you can print the values, as their values are stored in order in the list. If you have two sets of strings in each part of the story this should work:
for i, j story:
print(i, j)
I'm not sure as to how to print the different parts of the stories if they have different numbers of strings. This is because the "i, j" part of the for-loop prints the first two string of the list of strings. You could drop the commas between the different sentences in "Outside, Inside and Landing", whereas you could simply print it normally:
for i in list:
print(i)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to print firstname of employees who have both work number and mobile number. Below is my json body. I am facing difficulty in getting inside phoneNumbers attribute. My final output should be: "Adhi as Adhi has both work and mobile numbers".
I am not able to iterate the inner dictionary of phoneNumbers attribute.Can you please help me on this.
This is my python code
for i in Data['users']:
for j in i['phoneNumbers']:
for i in range(len(j)):
if j['type']=="work" and j['type']=="mobile":
print("Firstname",i['firstName'])
You can loop over the users and check if the work and mobile number are present:
for user in Data['users']:
has_mobile_number = False
has_work_number = False
for phonenumber in user['phoneNumbers']:
if phonenumber['type'] == 'work':
has_work_number = True
if phonenumber['type'] == 'mobile':
has_mobile_number = True
if has_work_number and has_mobile_number:
print('Firstname', user['firstName'])
Also, I recommend not using i and j when not talking about indexes. In you code, i is a dict representing a user and j is a dict representing a phone. I replaced them with user and phonenumber for more clarity in the code above.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
mother_dict=
{'son_dict':{'Name':'Jason','Age':26},'daughter_dict':{'Name':'Emma','Age':19}}
father_dict={}
father_dict['Child']=mother_dict[son_dict]
I need a way to replace father_dict['Child'] with a dictionary from mother_dict based on input.
I've tried deleting the contents of father_dict and replacing them with the contents of mother_dict with .update(), but that of course adds the whole dictionary, I've tried using input() to ask the user for a child, so if they said 'Jason' it would replace 'Child' with son_dict, but when I got into families with ten or so kids there would need to be ten functions, and if the children's names changed then both the functions and the dictionaries would need to be re-written. I'm hung up on using input to grab a specific dictionary from mother_dict and copying it to father_dict.
Maybe something like the following?
choice = ''
mother_dict= {'son_dict':{'Name':'Jason','Age':26},'daughter_dict':{'Name':'Emma','Age':19}}
father_dict = {}
while choice not in mother_dict:
choice = raw_input('Which dict do you want? ')
father_dict[choice] = mother_dict[choice]
This code gets input until the input is valid (it is in mother_dict), and then it adds that input to father_dict.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hey guys am new to python development..I am studying python on my way
I have just tested a simple code which includes assigning two variables with single at one line
Here is my snippet:
name = 1
somevariable = "hellow am new to python"
print somevariable[name]
And i got an output "e".
I did not understand what it means. I just tried out a random example .Is it allowed to do like this in python .or is it with arrays. Please help me to find an appropriate answer. Any help would be appreciated.
EDIt
Can we store a variable information to other variable in python
For eg
name = 1
age = 2
string = "yeah am a man"
name[age] = stringname = 1
My qus is that can we store the value 1 to age ?..AM new to python ..Sorry for the bad question
First of all you need to read basic of python first, because from your snippet clearly says that you don't know what is mutable and immutable object in python.
And for your question,this name[age] = stringname = 1 is not allowed.
First you will name Error for age after that you will get int object is not allowed for item assignment.
About list:
About Dictionary:
I'm not quite sure what you're trying to achieve, but it sounds a bit like you're trying to store multiple attributes (e.g name and age). If so, you could use a dict. e.g.
# initialise the dict
user = {}
# Add some data
user["name"] = "User"
user["age"] = 1
To retrieve the variables, just use e.g. user["name"]