简体   繁体   中英

How to unpack tuples with for loops

I am trying to unpack the values of my data tuple into the variables in the for loop, but it doesn't seem to be working. I am not sure if I've got the syntax right. The error says they are too many variables to unpack.

data=("Kareemah","18","coding")

for name,age,hobby in data:
    print(f"Here is some info about test subject 11.Her 
      name {name}\n"
     f"She is {age} years old \n"
     f"She likes {hobby}")

Your touple must be put in a list. This is one way to solve your issue:

data=[("Kareemah","18","coding")]

If it is just one tuple you can do this.

name,age,hobby = data

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