Python Forum
How to print multiple elements from multiple lists in a FOR loop?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to print multiple elements from multiple lists in a FOR loop?
#4
(Dec-01-2020, 11:17 PM)deanhystad Wrote: You don't handle empty questions correctly. Be careful about using recursion
question_list = []

def get_questions():
    question = input("Enter your question: ")
    if len(question) == 0:
        print("No Empty Questions.")
        get_questions()
    question_list.append(question)
    return question

for _ in range(3):
    get_questions()
print(question_list)
Output:
Enter your question: 1 Enter your question: No Empty Questions. Enter your question: 2 Enter your question: No Empty Questions. Enter your question: 3 ['1', '2', '', '3', ''] <-- Two blank questions
Personally I think a blank question is a great way to indicate you are done entering questions, but if you want to keep asking until a question is answered you should use something like this:
def get_questions():
    while True:
        question = input("Enter your question: ")
        if len(question) == 0:
            print("No Empty Questions.")
        else:
            question_list.append(question)
            return question

That's a cool solution! it doesn't fit the current build because I want the user to type the answers immidietely rather than stack them and then type the answers for them.
I will definetely use this solution in other stuff though!
thanks for your time!
Reply


Messages In This Thread
RE: How to print multiple elements from multiple lists in a FOR loop? - by Gilush - Dec-01-2020, 11:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Project Structure for Modularity and Reusability with Multiple Entry Points b19wh33l5 0 137 Apr-24-2024, 12:21 PM
Last Post: b19wh33l5
  Using a script to open multiple shells? SuchUmami 9 543 Apr-01-2024, 10:04 AM
Last Post: Gribouillis
  __init__() got multiple values for argument 'schema' dawid294 4 2,406 Jan-03-2024, 09:42 AM
Last Post: buran
  problem with print lists MarekGwozdz 4 697 Dec-15-2023, 09:13 AM
Last Post: Pedroski55
  python convert multiple files to multiple lists MCL169 6 1,568 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  Multiple variable inputs when only one is called for ChrisDall 2 496 Oct-20-2023, 07:43 PM
Last Post: deanhystad
  Can I use logging in a class (without multiple messages) mevan 2 608 Oct-16-2023, 11:08 PM
Last Post: mevan
  Search for multiple unknown 3 (2) Byte combinations in a file. lastyle 7 1,378 Aug-14-2023, 02:28 AM
Last Post: deanhystad
Question Using SQLAlchemy, prevent SQLite3 table update by multiple program instances Calab 3 761 Aug-09-2023, 05:51 PM
Last Post: Calab
  What's the best way for multiple modules to handle database activity? SuchUmami 3 660 Jul-08-2023, 05:52 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020