My break function isn't working and i dont know why its outside the loop

Question:

I am relatively new to code and have been messing around with some functions, i am currently using it for school work and have been practicing code for my course work, however when i tried to do this random code the break function was outside of the loop somehow but i dont know why, any ideas?

var=input("what name would you like to use?: ")
varf= len(var) + 1

def rep():
  for i in range(0, varf):
    print(var[:i])
    
  for x in range(0, varf):
    print(var[x:])
  
  repeat=input("Would you like to go again? Y/N: ")
  if repeat.upper() =="Y":
    rep()
  else:
    break
  
rep()
Asked By: asuno__

||

Answers:

The code you actually wanted to write goes as follows:

def rep():
  for i in range(0, varf):
    print(var[:i])
    
  for x in range(0, varf):
    print(var[x:])

while True: 
    var=input("what name would you like to use?: ")
    varf= len(var) + 1
    rep()
    repeat=input("Would you like to go again? Y/N: ")
    if repeat.upper() != "Y":
        break

Now the break statement is inside the while True: loop body and works as expected.

In your code you have just forgotten to put the user input into a while True: loop to be able to go for another round of user input.

Answered By: Claudio
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.