How to print out every input?
I'm suppose to write a code using arrays and inputs then split the list from people above 16 and people below 16. but can't seem to get the code to print every input. it only wants to print the last input I did. Here is what I have so far:
A = [0 for x in range (10)] B = [0 for x in range (10)] for i in range (10): A[i] = input('enter A name: ') B[i] = input('enter the age to go with the name: ') print() B2 = int(B[i]) if B2 > 16: print(B2, A[i]) if B2 < 16: print(A, B2)
That is because, you are overwriting
B2 in the line
B2 = int(B[i]) at every single iteration of
i inside the for loop (
for i in range (10): ) The following code should work:
A , B = ,  for i in range(10): A += [raw_input('enter A name: ')] B += [input('enter the age to go with the name: ')] print() for i in range(len(B)): if B[i] >= 16: print(B[i], A[i]) if B[i] < 16: print(A[i], B[i])
In the above code
A is a list which stores all the names as strings.
B is another list which stores all ages as
int data types.
print A >>>['Name1', 'Name2', 'Name3', 'Name4', 'Name5', 'Name6', 'Name7', 'Name8', 'Name9', 'Name10'] print B >>>[18, 10, 19, 5, 55, 12, 6, 66, 14, 7]
python - How to print out every input?, I'm suppose to write a code using arrays and inputs then split the list from people above 16 and people below 16. but can't seem to get the code to print every Every program is eventually a data processor, so we should know how to input and output data within it. There exists a function, print(), to output data from any Python program. To use it, pass a comma separated list of arguments that you want to print to the print() function. Let's see an example.
Your code does ignore 16y olds and has some other problems.
You can either filter the input directly after input into two seperate lists (
over_15) or simply throw all into one list (
all_studs) and filter them later using list comprehensions or filter-function :
under_16 =  # immediatly sorted over_15 =  # immediatly sorted all_studs = # all of them for i in range (10): name = input('enter A name: ') while True: # avoid input of "ten" when age is asked - ask until valid try: age = int(input('enter the age to go with the name: ')) break except Exception: print("Try again - input age. Hint: a _number_") # add all to big list all_studs.append((name,age)) # sort immediately on input if age < 16: under_16.append( (name,age)) else: over_15.append( (name,age)) # get from list by list-comp: under = [ (name,age) for name,age in all_studs if age < 16 ] over = [ (name,age) for name,age in all_studs if age >= 16 ] # get from list by filter: un = list(filter(lambda x:x<16,all_studs)) ov = list(filter(lambda x:x>15,all_studs)) print(un) print(ov) print(under) print(over) print(under_16) print(over_15) print(all_studs)
Output (for input
# un, under, under_16 [('Phil', 2), ('E', 15), ('F', 14), ('G', 13), ('H', 12), ('I', 11)] # ov, over, over_15 [('A', 19), ('B', 18), ('C', 17), ('D', 16)] # all_studs [('Phil', 2), ('A', 19), ('B', 18), ('C', 17), ('D', 16), ('E', 15), ('F', 14), ('G', 13), ('H', 12), ('I', 11)]
While Loops and Input, Print a new statement that lists all of the games that we like to play (we means you and your user). top. Using while loops to keep your programs running Most examples you see of C++ use the so-called stream output for the code. Stream output uses the << operator, as shown in this example: cout << "The user's name is " << sUserName << endl; However, C++ inherits another form of output from its predecessor, C.
It is because you are overwriting
B2 since the if condition is outside the for loop. Every time
B2 will get overwritten by what input user provides. I don't know what you are trying to do with the printing part . but putting it in loop should fix the problem.
Swift Basic Input and Output (With Examples), terminator: The string to print after all items have been printed. The default is a newline ("\n") . Since, last two Python Input. Up until now, our programs were static. The value of variables was defined or hard coded into the source code. To allow flexibility, we might want to take the input from the user. In Python, we have the input() function to allow this. The syntax for input() is: input([prompt])
Java Basic Input and Output, print(); or System.out.printf();. to send output to standard output (screen). Here,. System is a class Python Program to return Elements in a List Example 2. This python program is the same as above. But this time, we are using For Loop to iterate every element in a list, and printing that element.
7. Input and Output, Finally, you can do all the string handling yourself by using string slicing and print(hellos) 'hello, world\n' >>> # The argument to repr() may be any Python Fill out the labels. Depending on the labels you are using, some have spaces to fill where you can type specific information. This information could include things like company name, first & last name, and phone number, etc. Fill out the proper information in each label on the sheet.
1.10. Input and Output, person = input('Enter your name: ') print('Hello', person). Run the program. Before running it (with any made-up data), see if you can figure out what it will do:. Python print dictionary keys and values : In this tutorial, we will learn how to print the keys and values of a dictionary in python. For printing the keys and values, we can either iterate through the dictionary one by one and print all key-value pairs or we can print all keys or values at one go.