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
[
attachment=1705]
Please post code in bbtags.
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.
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])
What do you mean by "tuple"?
Hi,
i mean a list of items (values)
a = ['x', 'y', 0]
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.
Thanks... it's exactly why I need... But it's complicate for a beginner like me.
Regards.