[Tutor] A question

Alan Gauld alan.gauld at yahoo.co.uk
Thu Apr 2 06:00:48 EDT 2020


On 02/04/2020 09:49, Sara Mn wrote:
> Hello Sir/Madam,
> I have a question regarding passing arguments to another function, I have made this code fragment:
> 
> def question1():
>     print("Q1) What subject of the below subjects you find most intresting:\na-Biology\nb-Chemistry\nc-Math\nd-Physics\ne-Business Studies\nf-Computer Science\ng-Art")
>     answer1=input("Enter your answer's letter Or enter X to exit the program: ").lower()
>     if answer1 in  answerList1 :
>         return answer1
>     
>     elif answer1=='x':
>         sys.exit(0)
>    
>     else:
>         while True:
>             print("\nThe answer you entred is invalid, you have two more tries. If you wish to exit the program enter X")
>             answer1=input("Enter your answer: ")
>             if answer1 in answerList1 :
>                 
>                 return answer1
>                 break
>             elif answer1=='x':
>                 sys.exit(0)
>             else:
>                 print("The answer you entred is invalid, you have one more try. If you wish to exit the program enter X")
>                 answer1=input("Enter your answer: ")
>                 return answer1 
>             if answer1 in answerList1:
>                 
>                 return answer1 
>                 break
>             elif answer1=='x':
>                 sys.exit(0)
>                 
>             else:
>                 print("The answer you entred is invalid. The program is being terminated as you have reached your maximum number of tries") 
>                 sys.exit(0)

Notice tat you are repeating what is almost identical code multiple times.
You should be able to restructure your function so you only do things
once(or at most twice!). (It might help to split it into two functions,
one to get the input and the other to monitor how many tries.)

Also you should be able to pass in the question so that you only need
one function not one per question. That will make your code easier to
read and maintain/modify.

> def AnswerCollection():
>     n1=question1()
>     print(n1)

> However I can’t just pass the local variable answer1 to the function AnswerCollection() 
> it passes the whole function with the question.

I don't understand what you are trying to do.
answer1 only exists inside question1() so you can only use it
if you are calling AnswerCollection from inside question1.
But you call question1 from inside AnswerCollection?

It might help if you post the code where you try to pass the
parameter and any error messages so we can see what you are
trying to do. At the moment it is not clear.

> What can I do to pass only the local variable. Also, I have 6 different 
> functions with the same format.> How can I pass 6 different local variables from 6 different functions
to one function.

Again I'm not sure what you mean. If you can show us - even with just
two functions instead of 6...

In general a function has a number of input parameters. You can call
that functio from as many places as you like passing in the required
values. The function doesn't know or care where it is called from, it
just works with what you give it. Of course one of the parameters can be
a list, so if you build the list and pass it each time then the function
can see the previous values. But I don't know if that's what you want?



-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list