Posts: 1
Threads: 1
Joined: Apr 2017
Apr-05-2017, 06:24 PM
(This post was last modified: Apr-05-2017, 06:24 PM by BlathaBlather.)
Say I had a list called 'rade' and in it was [1,2, 3, 4, 5,] and I also had another list called 'spam' and in it was [6, 7, 8, 9, 10,]
I want to input that I want to use the list 'rade'. How can I input it so it would work?
1 2 3 4 5 6 7 |
rade = [ 1 , 2 , 3 , 4 , 5 , ]
spam = [ 6 , 7 , 8 , 9 , 10 ]
choice = input ( "Chose a list" )
if choice[ 1 ] = = 2 :
print ( "Congratulations" )
else :
print ( "Sorry..." )
|
Posts: 12,038
Threads: 487
Joined: Sep 2016
Your if statement has nothing to do with your question
From your question, I would expect the input to be either rade of spam.
if you want to input an integer I would suggest:
1 2 3 4 5 6 |
try :
choice = 0
while choice not = 1 and choice not = 2 :
choice = int ( input ('Please enter 1 for rade, 2 for spam: ))
except ValueError:
print ( 'Requested 1 or 2' )
|
Posts: 2,953
Threads: 48
Joined: Sep 2016
(Apr-05-2017, 06:29 PM)Larz60+ Wrote: Your if statement has nothing to do with your question
From your question, I would expect the input to be either rade of spam.
if you want to input an integer I would suggest:
1 2 3 4 5 6 |
try :
choice = 0
while choice not = 1 and choice not = 2 :
choice = int ( input ( 'Please enter 1 for rade, 2 for spam: )) # the second ' is missing
except ValueError:
print ( 'Requested 1 or 2' )
|
Posts: 687
Threads: 37
Joined: Sep 2016
(Apr-05-2017, 06:24 PM)BlathaBlather Wrote: Say I had a list called 'rade' and in it was [1,2, 3, 4, 5,] and I also had another list called 'spam' and in it was [6, 7, 8, 9, 10,]
I want to input that I want to use the list 'rade'. How can I input it so it would work?
1 2 3 4 5 6 7 |
rade = [ 1 , 2 , 3 , 4 , 5 , ]
spam = [ 6 , 7 , 8 , 9 , 10 ]
choice = input ( "Chose a list" )
if choice[ 1 ] = = 2 :
print ( "Congratulations" )
else :
print ( "Sorry..." )
|
Neither... it's a very bad idea to code it like this. What you can do is either use an array:
1 2 3 |
lists = [[ 1 , 2 , 3 , 4 , 5 , ],[ 6 , 7 , 8 , 9 , 10 ]]
list_number = int ( input ( "Chose a list: 1 for rade, 2 for spam" ))
chosen_list = lists[list_number - 1 ]
|
... or a dictionary:
1 2 3 |
lists = { "rade" :[ 1 , 2 , 3 , 4 , 5 , ], "spam" :[ 6 , 7 , 8 , 9 , 10 ]}
list_name = input ( "Chose a list among: " + ',' .join(lists.keys()) + ": " )
chosen_list = lists[list_name]
|
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
|