Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
data input
#6
(May-06-2020, 10:01 PM)chpyel Wrote: and other nine for another input ?
If you need to do this over and over, create it into a function. Functions are for repeated chunks of code, splitting logic, and organizing code. So if you need to collect input multiple times then you can make it into a function. Your code might look something like this
def get_input(message):
    #Insert code that I wrote earlier

def main():
    integer1 = get_input("Type your first integer: ")
    integer2 = get_input("Type your second integer: ")
    integer3 = get_input("Type your third integer: ")
    integer4 = get_input("Type your fourth integer: ")
    #Do something with them

if __name__ == "__main__": #If this code is the main running code (not being imported)
    main()
You could simplify it further with a dictionary
def get_input(message):
    #Insert code that I wrote earlier

def main():
    intDict = {}
    numberOfIntegers = get_input("How many integers do you want? ")
    for num in range(0, numberOfIntegers):
        intDict.update({"Integer %s" %(num+1): get_input("Type integer number %s: " %(num+1))})
    #Do something with the dictionary of integers

if __name__ == "__main__":
    main()
This way you could get up to 100 integers efficiently. You can even get the user to separate it into commas and then use string.split() where string would be the input.
Hope this helps.
Reply


Messages In This Thread
data input - by chpyel - May-06-2020, 08:52 PM
RE: data input - by Larz60+ - May-06-2020, 09:20 PM
RE: data input - by chpyel - May-06-2020, 09:27 PM
RE: data input - by SheeppOSU - May-06-2020, 09:42 PM
RE: data input - by chpyel - May-06-2020, 10:01 PM
RE: data input - by SheeppOSU - May-06-2020, 10:18 PM
RE: data input - by jefsummers - May-06-2020, 10:31 PM
RE: data input - by chpyel - May-06-2020, 10:39 PM
RE: data input - by perfringo - May-07-2020, 05:16 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 585 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  manually input data jpatierno 0 414 Nov-10-2023, 02:32 AM
Last Post: jpatierno
  Input network device connection info from data file edroche3rd 6 1,222 Oct-12-2023, 02:18 AM
Last Post: edroche3rd
Question in this code, I input Key_word, it can not find although all data was exact Help me! duchien04x4 3 1,167 Aug-31-2023, 05:36 PM
Last Post: deanhystad
  Showing an empty chart, then input data via function kgall89 0 1,035 Jun-02-2022, 01:53 AM
Last Post: kgall89
Question Change elements of array based on position of input data Cola_Reb 6 2,250 May-13-2022, 12:57 PM
Last Post: Cola_Reb
  input data validation plumberpy 2 1,881 Aug-11-2021, 12:04 PM
Last Post: plumberpy
  command line input (arg parse) and data exchange Simba 7 4,434 Dec-06-2019, 11:58 PM
Last Post: Simba
  user input to select and print data from another class python TyTheChosenOne 6 4,287 Aug-30-2018, 05:53 PM
Last Post: TyTheChosenOne
  Input Data machine Learning rafaelmoraes 2 3,393 Mar-15-2018, 01:24 PM
Last Post: rafaelmoraes

Forum Jump:

User Panel Messages

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