Python Forum

Full Version: Unable to implement loop count
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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()  
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:
Output:
arz60p@linux-nnem: src:$python ziggy.py Name of Solution: 'quit' to stop: Jeff Name of Solution: 'quit' to stop: Mary Name of Solution: 'quit' to stop: Susan Name of Solution: 'quit' to stop: Glugger Name of Solution: 'quit' to stop: Toff Name of Solution: 'quit' to stop: quit ['Jeff', 'Mary', 'Susan', 'Glugger', 'Toff']
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.
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.