I am trying to get a program to run if you dont type a number, but i dont get it. Can anyone help?
loop=True
print("Velcome to the pythagorean triples generator, if you want the machine to stop type stop")
print("Choose an uneven number over 1")
while loop:
number1 = input("write here: ")
try:
number = int(number1)
except:
print("It has to be a whole number")
if int(number)%2 == 0 or int(number)==1
print("the number has to be uneven and bigger than 1")
else:
calculation = int(number) ** 2
calculation2 = int(calculation) - 1
calculation3 = int(calculation2) / 2
print ("Here is your first number,", int(calculation3))
calculation4 = int(calculation3) + 1
print ("Here is your second number,", int(calculation4))
if str(tal) == "stop":
break
Edit: translated
I'm guessing that you want your program to continue asking for a number when the user's input it not a number.
If this is the case, which you should clarify, then this will do the trick:
except:
print("It has to be a whole number")
continue
The continue
keyword skips the current iteration and continues with the next iteration. The continue keyword in Python
Without doing this your code will print "Must be a number" in your exception, but will continue execution at the next line where you try to convert number
, which at this point we know can't be converted to an int
, thereby causing an unhandled exception. This will be solved by using the continue
keyword as I suggested.
However, if there was no exception raised (i.e. the input can be interpreted as an int
), than there is absolutely no point in saying int(number)
as number
at this point is already an int
!