By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,846 Members | 1,862 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,846 IT Pros & Developers. It's quick & easy.

Assigning multiple variables in an input statement

P: 3
Ok, here goes
I was wondering if there was a better way to do this. I want my final program to run like this for the user:

What did x get? <user input>
What did y get? <user input>
What did z get? <user input>

x received <users answer>
y received <users answer>
z received <users answer>

I'm aware of two ways of doing this but they both seem rather long winded. One way is the following:
Expand|Select|Wrap|Line Numbers
  1. def main ():
  2.     x = input('What did x get? ')
  3.     y = input('What did y get? ')
  4.     z = input('What did z get? ')
  5.  
  6.     print ('x received ') x
  7.     print ('y received ') y
  8.     print ('z received ') z
  9. main ()
  10.  
The second is by making a function for the input statement like this:
Expand|Select|Wrap|Line Numbers
  1. def main ():
  2.     x = ask_question ('x')
  3.     y = ask_question ('y')
  4.     z = ask_question ('z')
  5.  
  6.     print ('x received ') x
  7.     print ('y received ') y
  8.     print ('z received ') z
  9.  
  10. def ask_question (letter):
  11.     answer = input ('What did %s receive? ' % letter)
  12. return answer
  13.  
  14. main ()
  15.  
So i guess what i'm asking is it possible to do this in a simpler way? like:
Expand|Select|Wrap|Line Numbers
  1. def main ():
  2.     letters = 'x','y','z'
  3.     x,y,z = input (What did %s receive? ' % letters)
  4.  
  5.     print ('%s received ' % letters) x,y,z
  6. main ()
  7.  
I know that doesn't work, just thought it would help everyone to understand where i'm coming from. Thanks :)
Apr 12 '11 #1

✓ answered by bvdet

I might do something like this (Python 2.3 code):
Expand|Select|Wrap|Line Numbers
  1. class GetInput:
  2.  
  3.     def __init__(self, *attrs):
  4.         self.attrs = attrs
  5.         for attr in attrs:
  6.             setattr(self, attr, raw_input("Enter value for attribute '%s'" % (attr)))
  7.  
  8.     def getvars(self):
  9.         return [getattr(self, attr, None) for attr in self.attrs]
  10.  
  11. obj = GetInput('x', 'y', 'z')
  12. x,y,z = obj.getvars()
  13.  
  14. print x, y, z

Share this Question
Share on Google+
1 Reply


bvdet
Expert Mod 2.5K+
P: 2,851
I might do something like this (Python 2.3 code):
Expand|Select|Wrap|Line Numbers
  1. class GetInput:
  2.  
  3.     def __init__(self, *attrs):
  4.         self.attrs = attrs
  5.         for attr in attrs:
  6.             setattr(self, attr, raw_input("Enter value for attribute '%s'" % (attr)))
  7.  
  8.     def getvars(self):
  9.         return [getattr(self, attr, None) for attr in self.attrs]
  10.  
  11. obj = GetInput('x', 'y', 'z')
  12. x,y,z = obj.getvars()
  13.  
  14. print x, y, z
Apr 15 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.