(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.