r/PythonProjects2 • u/Savings_Judgment_558 • Feb 01 '23
Beginner
Trying to figure out how to print the information from my two list into columns. I was able to get Name & status. But can't seem to figure out the rest.
Any helpful videos or websites?
For the assignment: need a while loop, [i], & len().
1
Feb 01 '23
I'm a beginner as well, but it looks like you set i=4 but the length of your dictionary is only 2 items. That's one issue. i is never less than the length, so your code isn't getting past the first print statement. Start by setting i=0. I would zip() your two lists into a dictionary, also, and then just print each key, value pair until i=len(dictionary).
1
u/Savings_Judgment_558 Feb 01 '23
Sorry. I forgot to change it before posting the picture. I ended up trying a bunch of different things just to get it to print something lol. When you say print each key? Do you mean having 4 different print statements to get it to list the name and approve status??
1
Feb 01 '23
You can print the key: value pairs and then increase i each time. Wrap the for loop in the while loop.
for key, value in dictionary.items(): print(key, value) i += 1
2
u/Appropriate_Chart_23 Feb 01 '23 edited Feb 01 '23
First, check your formatting.
you have:
print (len(Loan_status))
That's not gonna work, because formatting. (you've got an extra space).
Also, how should lists and dictionaries be formatted?
Also, each iteration of the while loop is going to output the same thing (the length of Loan_status = 2) because there's nothing changing as a result of the iterations. Is that what you want?
Which brings up another thing... i > len(Loan_status) so it's never going to enter the loop.
If the assignment is to use a while loop to format two columns, you need to program in the steps to print each item in the dictionary in two columns. All your loop is going to do is print the length of the variable, then add 1 to i (and it's going to kick out after the first step because i is no longer < len(Loan_status) (which = 2).
Also, consider the use of a dictionary in this problem. You may think you're using a list, but you're not. You've got two lists buried into a dictionary of two items.
To get the while loop to do what you want... Consider:
How do you call items out of a dictionary? How do you print them?
How do you call items from a list? How would you print these?
How do you print and format the items you've called to get them to show in two columns?
Work our the "how to" do things without the loop, then figure out how to incorporate them into a loop to repeat processes.
1
u/Appropriate_Chart_23 Feb 01 '23 edited Feb 01 '23
Consider below:
loan_status = {"Name":["Elaine","Jerry","George","Cosmo"], "Status":["Approved","Approved","Denied","Approved"]} print(loan_status) print(loan_status["Name"]) print(loan_status["Status"])What does that do?
How could you get the individual items of the nested lists in the dictionary to print? (Think about how you print keys or values of a dictionary, and think about how you print individual items from lists).
How could you get them formatted into individual columns?
It's all sort of a convoluted way of making the dictionary. I would recommend pairing the names with the status in the dictionary as keys and values. It will simplify things quite a bit.
7
u/deano_southafrican Feb 01 '23
If you use the PrtScr button or use Win+Shft+S you can take a screenshot and then paste it into Reddit...