Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Inputting a varible
#1
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...")
Reply
#2
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:
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')
Reply
#3
(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:
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')
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  local varible referenced before assignment SC4R 6 1,532 Jan-10-2023, 10:58 PM
Last Post: snippsat
  Python 3.8 Nested varible not updating Teknohead23 6 2,448 Oct-02-2021, 11:49 AM
Last Post: Teknohead23
  Unable to understand how given code takes a fixed input value, without inputting. jahuja73 4 2,713 Jan-28-2021, 05:22 PM
Last Post: snippsat
  Argparse error when inputting values tqader 2 2,891 Sep-11-2020, 07:42 PM
Last Post: buran
  Letters with Accents Not Inputting Phylus 0 1,466 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