Unable to implement loop count - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Unable to implement loop count (/thread-11787.html) |
Unable to implement loop count - zukochew - Jul-25-2018 Hello all, I have difficulty trying to count and print number of loops and implement it as solution number. The codes are below. Any help would be appreciated! def solutionnames(): numofsol= input("How many solutions are there?") listofnames= [] loops=0 for i in range(int(numofsol)): loops +=1 NamesofSol=input("Name of Solution",str(loops)) listofnames.extend([NamesofSol]) print(listofnames) return(listofnames) solutionnames() RE: Unable to implement loop count - Larz60+ - Jul-25-2018 you are going about this the wrong way: def solutionnames(): listofnames = [] while True: name = (input("Name of Solution: 'quit' to stop: ")) if name == 'quit': break listofnames.append(name) return(listofnames) if __name__ == '__main__': print(solutionnames())test:
RE: Unable to implement loop count - zukochew - Jul-25-2018 Hello Larz60+, Thanks for replying to my request. However, my desired outcome is Name of Solution 1: 'quit' to stop: Jeff Name of Solution 2: 'quit' to stop: Mary Name of Solution 3: 'quit' to stop: Susan Name of Solution 4: 'quit' to stop: Glugger Name of Solution 5: 'quit' to stop: Toff Name of Solution: 'quit' to stop: quit The difference is the number input after "Name of Solution". I apologise for not clarifying beforehand. RE: Unable to implement loop count - ichabod801 - Jul-25-2018 Use string formatting: input("Name of Solution {}: 'quit' to stop: ".format(loops))Format puts the item passed to it in place of the braces ({}). If you pass it multiple arguments, it can replace multiple braces. And far more complicated stuff. |