0

Im just starting to learn python and was wondering if there was a better way to code the following.

user1 = "username"
userkey1 = "userkey"
user2 = "username"
userkey2 = "userkey"
user3 = "username"
userkey3 = "userkey"
user4 = "username"
userkey4 = "userkey"
user5 = "username"
userkey5 = "userkey"
user6 = "username"
userkey6 = "userkey"
user7 = "username"
userkey7 = "userkey"

level = ["variable2", "variable2"]

connect = ipaddress
for article in connect.link("var1", "var2"):
    if article["variable"] == '' and article["creator"] in level:
        try:
            action = dr.create(
                    **{"person": user1,}
            )

            output = output.sign([userkey1])

            output = tx.JsonObj(tx)

            # Broadcast to network
            broadcast

    if article["variable"] == '' and article["creator"] in level:
        try:
            action = dr.create(
                    **{"person": user2,}
            )

            output = output.sign([userkey2])

            output = tx.JsonObj(tx)

            # Broadcast to network
            broadcast

    if article["variable"] == '' and article["creator"] in level:
        try:
            action = dr.create(
                    **{"person": user3,}
            )

            output = output.sign([userkey3])

            output = tx.JsonObj(tx)

            # Broadcast to network
            broadcast

I am not yet familiar whit while loops, loops or arrays, but it seems to me the code above can be a bit more efficient?

The code is supposed to grab input and just cycle always, when it finds what its looking for its supposed to create an action for each user and their respective key.

Any help would be appreciated.

  • 3
    If your code *works*, ask for optimization/advice on http://codereview.stackexchange.com/ – Idos Jul 26 '16 at 11:42
  • 2
    Use of variables like `user4`, `user5` etc. is almost always an indicator that a list called `user` should be defined instead. – John Coleman Jul 26 '16 at 11:43
  • Store the users in a [list](https://docs.python.org/2/tutorial/introduction.html#lists) or [dict](https://docs.python.org/2/tutorial/datastructures.html#dictionaries). – Aran-Fey Jul 26 '16 at 11:44
  • 1
    You are testing exactly the same condition three times. Also, `broadcast` should probably be `broadcast()` – tobias_k Jul 26 '16 at 11:44
  • Yeah the original code works but contained sensitive information so I modified it in order to post. what Im most interested in is knowing whether it needs a "try" for each user or if there is a way to create an array and the "try" would just loop how ever many times it needs to to cycle through the users. – Don Fantasia Jul 26 '16 at 11:53
  • try to describe a bit more what you are trying to achieve. it is not clear from your code what your goal is and the "optimal" version of your code will depend on that. – grepe Jul 26 '16 at 13:26

1 Answers1

0

If your code is working the way you want it good. But the long variable definitions is to much. Look up (google) python list and dictionary. Check out this post: How to implement associative array in Python?

Community
  • 1
  • 1
Just Ice
  • 179
  • 1
  • 1
  • 11