'it doesn't loop but just breaks even if i input Y
def interact():
while True:
try:
num = int(input("Please input an integer: "))
if (num % 2) == 0:
print ("{0} is even".format(num))
else:
print("{0} is odd".format(num))
num_two = int(input('Do you want to play again n/Y:'))
except:
if num_two == "y":
continue
finally:
print("Goodbye")
break
Solution 1:[1]
def interact():
while True:
try:
num = int(input("Please input an integer: "))
if (num % 2) == 0: print ("{0} is even".format(num))
else: print("{0} is odd".format(num))
num_two = int(input('Do you want to play again n/Y:'))
except:
if num_two == "y":
continue
finally:
print("Goodbye")
break
the problem with this code is that the 'break' makes it so that you are out of the while true loop (i think)
therefore i think removing the 'break' might solve it?
Solution 2:[2]
You must add specific error to except and remove break from finally:
try:
user_input = input()
int(user_input)
except ValueError:
print('not a number')
if user_input = 'y':
continue
else:
break
finally:
print('Finally')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | DialFrost |
Solution 2 | marc_s |