简体   繁体   中英

Python, Better way of coding. Using Loop Array?

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM