0

What im trying to do is have the user type a number, which would then register into the system as a specific variable.

Example:

n1 = "X"
n2 = "Y"
n3 = "Z"
num = input("enter a number (1-3): ")
print(n(num))

So, if the user entered the number 2 into their program, the program would display the value stored in n2, or be able to use n2 in an equasion.

Is this possible? I'm still new to Python and this is not a school assignment, just my own curiosity :)

Thanks

EDIT:

Here is what im trying to do:

temp = int(input("\nPlayer One, please pick a square (1-9): "))
while {1:n1, 2:n2, 3:n3, 4:n4, 5:n5, 6:n6, 7:n7, 8:n8, 9:n9}[temp] == "X" or {1:n1, 2:n2, 3:n3, 4:n4, 5:n5, 6:n6, 7:n7, 8:n8, 9:n9}[temp] == "O":
    temp = str(input("\nPlayer One, please pick a valid square (1-9): "));
{1:n1, 2:n2, 3:n3, 4:n4, 5:n5, 6:n6, 7:n7, 8:n8, 9:n9}[temp] = "X"
user2462068
  • 161
  • 2
  • 2
  • 9

1 Answers1

2

You could use a dictionary for this. Like:

num = input("...")
print {1:n1, 2:n2, 3:n3}[num]

Hope that helps.

Fernando Aires
  • 532
  • 2
  • 7
  • Hmmm, that looks interesting.. is there a way I can use this as instead of having an if statement for n1, elif for n2 and so on down the list, I could just have one if statement that will adapt depending on the user? – user2462068 Oct 08 '14 at 23:01
  • I don't think I get what you intended to ask (I'm a foreigner, sorry), but dictionaries are used to select a value based on other value (like a "switch statement for values instead of commands") exactly as I did there. – Fernando Aires Oct 08 '14 at 23:04