userinput = input("Enter your name: ")
names = []
My question is, how to store the input to the list?
I tried to add it like a integer, but it doesn't work.
since names is a list, you want to append the input to it.
userinput = input("Enter your name: ")
names = []
names.append(userinput)
print(names)
names.append(userinput) you this code and should work
If you want to put all answers to a list, you can use append() function.
userinput = input("Enter your name: ")
names = []
names.append(userinput)
int(userinput) makes it an int and [] makes it a seperate list so you can append it instead of adding it.
userinput = input("Enter your name: ")
names = []
names += [int(userinput)]
You could use the append method. Example:
names = []
while True:
name = input("Enter a name or <enter> if you are finished: ")
if name == '':
break
else:
names.append(name)
print(f'You entered {names}')
list1 =["Jack","Donald"]
newname = input("Enter new name: ")
list1.append(newname)
Related
here is my code:
incorrect_names = []
name = "John"
while True:
name = input("Enter your name:")
incorrect_names.append(name)
print(incorrect_names)
if name == "John":
print("user enters John")
break
Only input that does not match the criteria is supposed to be in a list. In this case, only names other than John should be in a list. But in my code when I input John it displays the name in a list as well.
The if statement needs to be before you append to the list.
while True:
name = input("Enter your name:")
if name == "John":
print("user enters John")
break
incorrect_names.append(name)
print(incorrect_names)
I'm learning Python and usage of the stackoverflow so that's why this question might be trivial for you.
So; Code goal is to ask user names until user press enter. After that code should count how many names were given and then print list of the names.
Example
Enter a name:Jack
Enter a name:Dack
Enter a name:Nack
Enter a name:
Name count 3
Jack, Dack, Nack
I have created While true loop for name counting, but now I just can figure out how to simply print those names like in the above.
My code is:
count_names = 0
while True:
given_names = input("Enter a name: ")
if given_names == "":
print(f"Name count {count_names}")
break
count_names += 1
Result for that is:
Enter a name:Jack
Enter a name:Dack
Enter a name:Nack
Enter a name:
Name count 3
I can feel that the answer is right there but I just can't put my finger on it =/
Thanks again for the help.
Here is another way to print the result
count_names = 0
given_names, result = "abc", ""
while given_names != "":
given_names = input("Enter a name: ")
result += f"{given_names}, "
count_names += 1
print(f"Name count {count_names}")
print(result.rstrip(", "))
You can do it like this:
count_names = 0
names = []
while True:
given_names = input("Enter a name: ")
names.append(given_names)
if given_names == "":
print(f"Name count {count_names}")
names.remove('')
break
count_names += 1
for name in names:
print(name, end=', ')
Using lists is the best idea in terms of simplicity. I also always use join() method to separate list elements by comma.
nameList = []
count = 0
while True:
name = input('Enter a name: ')
if name:
nameList.append(name)
count += 1
else:
print(f'Name count: {count}')
print(', '.join(nameList))
break
Try the below code, it will return as you want
names = ''
count_names = 0
while True:
given_names = input("Enter a name: ")
if given_names == "":
print(f"Name count {count_names}")
print(names[:-1])
break
names += given_names + ','
count_names += 1
I want to create a simple app to Ask user to give name and marks of 10 different students and Store them in dictionary.
so far only key:value from the last input is stored into dictionary.
can you please check my code?
marks = {}
for i in range(10):
student_name = input("Enter student's name: ")
student_mark = input("Enter student's mark: ")
marks = {student_name.title():student_mark}
print(marks)
Your current code has two issues: first, it doesn't save the values of each student inside the loop. Second, it always rewrites the entire dictionary with a single key/value pair, that's why it doesn't work.
marks = {}
for i in range(10):
student_name = input("Enter student's name: ")
student_mark = input("Enter student's mark: ")
marks[student_name.title()] = student_mark
print(marks)
You need your code to be inside your loop. Also the way you put value inside your dict is not right. This should do the trick
marks = {}
for i in range(10):
student_name = input("Enter student's name: ")
student_mark = input("Enter student's mark: ")
marks[student_name] = student_mark
print(marks)
I'm trying to take information from the user and then take that info to update a dictionary. It's a part of an exercise I'm doing. I've tried to use .format(var1, var2) with the way you update a dictionary, but when ends up happening is only one part of the dictionary gets updated.
name_grade = {}
while True:
name = raw_input("Please give me the name of the student [q to quit]:")
if name == 'q':
break
else:
grade = raw_input("Give me their grade: ")
name_grade['{}'] = '{}'.format(name, grade)
name_grades = {}
while True:
name = input("Please give me the name of the student [q to quit]:")
if name == 'q':
break
else:
grade = input("Give me their grade: ")
name_grades[name]=grade
print(name_grades)
Are you looking for this or something else?
You can update the dictionary by saying
name_grade[name] = grade
I recently started learning coding. On my first assignment I was asked to make a program that allows the user to choose names and then pick random ones. Here's what I came up with:
def main():
name = ["josh", "omar", "shatil", "cathrin"]
while True:
print (name)
continueProgram = input("would you like to continue")
if continueProgram.lower() =="yes":
print(changeList(name))
else:
break
def changeList(items):
changeList= input("Enter 'a' to append list, 'r' to remove an item from the list: ")
if changeList.lower() == "a":
appendList = input("what name would you like to add to the list?: ") items.append(appendList)
if changeList.lower() =="r":
removeList = input ("what name would you liket to remove from the list?: ")items.remove(removeList)
return items
main()
The last 2 if statements aren't working.
Your indenting looks suspicious.
Instead of this:
def changeList(items):
changeList= input("Enter 'a' to append list, 'r' to remove an item from the list: ")
if changeList.lower() == "a":
appendList = input("what name would you like to add to the list?: ") items.append(appendList)
if changeList.lower() =="r":
removeList = input ("what name would you liket to remove from the list?: ")items.remove(removeList)
return items
Try this:
def changeList(items):
changeList= input("Enter 'a' to append list, 'r' to remove an item from the list: ")
if changeList.lower() == "a":
appendList = input("what name would you like to add to the list?: ")
items.append(appendList)
if changeList.lower() =="r":
removeList = input ("what name would you liket to remove from the list?: ")
items.remove(removeList)
return items