0

I am wanting to loop through 5 questions from another .py file.

The extra file is file.py and has 5 questions -q1 = "what is...", q2 etc.. This adds a number to each time I use q but does not catch the question from the external python document:

from file import*
i = 0
for x in range(5):
    i = i+1
    question1 = str(input("q" + str(i)))
2
  • This is why numbered variable names are discouraged. Defining q = ["what is...", "where is...", "who is..."] would allow you to use q[0], q[1], q[2]. Commented Nov 16, 2017 at 10:36
  • Hi Skoobay, if my answer helped you, could you consider marking it as accepted? Thanks! Commented Nov 20, 2017 at 8:54

1 Answer 1

2

The best way is to save the questions in file.py as a list as suggested in the comments. Then the contents of file.py would be:

q = ['who is...', 'What is...']

and the content of your program would be:

from file import q
for item in q:
    print(item)

If you want to do it your way, do it like this:

from file import *

for x in range(5):
    exec('question = q' + str(x+1))
    print(question)

However, this is not pythonic and not recommended.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.