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