Python Forum
SOLVED variable as tuple name
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SOLVED variable as tuple name
#1
Hi all,
I'm newbie in python coding.
Can somebody help me... i've search for a solution without success...
The problem is in the iomage...
Thanks.
Regards
   
buran write Apr-08-2022, 06:26 PM:
Please, don't post images of code, error, data, etc. Copy/paste as text in proper BBcode tags.
Reply
#2
Please post code in bbtags.
krayon70 likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
Please post code in the future.

The code is doing the correct thing for the input "a0". lettre is "a" and chiffre is 0. "a"[0] is "a".

If you entered "a1" you will get an exception. littre is "a" and chiffre is 1. "a"[1] is an Index Error.

You will get a Value Error if you enter "0a" because tir[1:2] is "a" and int("a") is a Value Error.

Why are you using tir[0:1] instead of tir[0]: The result is the same.
krayon70 likes this post
Reply
#4
Hi,
Sorry for posting an image...
I want the user can set the tuple and the value (position) to print.
Ie. if the user type a0, i want to print the first value from the tuple 'a' that is 'x' but it's not workink...
In the future, I will use several tuples.
Thanks for helping me.
Regards

a = ['x', 'y', 0]
tir = input("tir ? (ABC, 123 ; ex. A3) ")
lettre = tir[0]
chiffre = int(tir[1])
print(lettre)
print(chiffre)
print(lettre[chiffre])
Yoriz write Apr-09-2022, 07:31 PM:
Please post all code, output and errors (In their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#5
What do you mean by "tuple"?
Reply
#6
Hi,
i mean a list of items (values)
a = ['x', 'y', 0]
Reply
#7
I think I understand. You are wondering how you can use the user input to lookup a variable. You can use locals() which returns a dictionary of local variables.
a = ['x', 'y', 0]
while True:
    things = list(map(str.strip, input("Enter name, index ").split(",")))
    if len(things) < 2:
        break
    name, index, *_ = things
    print(locals()[name][int(index)])
Output:
Enter name, index a, 0 x Enter name, index a, 1 y Enter name, index a, 2 0 Enter name, index
I do not think this is a good idea. Instead of having several tuple variables you should have a tuple dictionary, just like you get from locals(). The advantage of using your own dictionary is you limit what the user can access. If you later modified you code so it could not only print things, but set things, locals() would you set any local variable. If you make a dictionary of tuples, the only thing the user could set would be values in the safely controlled tuple dictionary.

This is a list: ['x', 'y', 0]. This is a tuple ('x', 'y', 0). Lists are mutable. You can change the values in a list. tuples are immutable. You cannot change the contents of a tuple.
krayon70 likes this post
Reply
#8
Thanks... it's exactly why I need... But it's complicate for a beginner like me.
Regards.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Get latest version off website and save it as variable [SOLVED] AlphaInc 5 2,035 Nov-14-2021, 09:00 PM
Last Post: DeaD_EyE
  [solved] subdictionaries path as variable paul18fr 4 2,692 May-18-2021, 08:12 AM
Last Post: DeaD_EyE
  [solved] Variable number of dictionnaries as argument in def() paul18fr 11 6,243 Apr-20-2021, 11:15 AM
Last Post: paul18fr
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,890 Nov-04-2020, 11:26 AM
Last Post: Aggam
  How to get first line of a tuple and the third item in its tuple. Need Help, Anybody? SukhmeetSingh 5 3,278 May-21-2019, 11:39 AM
Last Post: avorane

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020