Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Inputting a varible
#4
(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?

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:
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:
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
Reply


Messages In This Thread
Inputting a varible - by BlathaBlather - Apr-05-2017, 06:24 PM
RE: Inputting a varible - by Larz60+ - Apr-05-2017, 06:29 PM
RE: Inputting a varible - by wavic - Apr-05-2017, 06:34 PM
RE: Inputting a varible - by Ofnuts - Apr-05-2017, 08:09 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  local varible referenced before assignment SC4R 6 3,170 Jan-10-2023, 10:58 PM
Last Post: snippsat
  Python 3.8 Nested varible not updating Teknohead23 6 3,593 Oct-02-2021, 11:49 AM
Last Post: Teknohead23
  Unable to understand how given code takes a fixed input value, without inputting. jahuja73 4 3,937 Jan-28-2021, 05:22 PM
Last Post: snippsat
  Argparse error when inputting values tqader 2 4,001 Sep-11-2020, 07:42 PM
Last Post: buran
  Letters with Accents Not Inputting Phylus 0 1,873 Jun-12-2019, 04:05 PM
Last Post: Phylus

Forum Jump:

User Panel Messages

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