-2

So I am trying to make a calculator on python, and am getting an error saying "'continue' not properly in loop"

Here is the code:

try:
  num1m = int(input("Select the first number --> "))
except ValueError:
  print("That's no number!")
  continue
try:
  num2m = int(input("Select the second number --> "))
except ValueError:
  print("That's no number!")
  continue
num3m = (num1m * num2m)
str(num3m)
print("The sum is " + num3m + ".")

Can someone help me out, thanks :)

Kevin
  • 72,202
  • 12
  • 116
  • 152
LokiH
  • 3
  • 3
  • 2
    Continue is a keyword for loop do do next iteration. – Benjamin Feb 10 '16 at 18:19
  • 1
    If you're trying to use "continue" as a command to go back a few lines and ask the user to re-enter their number, this may be useful to you: [Asking the user for input until they give a valid response](http://stackoverflow.com/q/23294658/953482) – Kevin Feb 10 '16 at 18:22
  • LokiH if any answer solved your question please accept it by clicking the green check mark next to it (on the left), it helps the community. Thank you – Idos Feb 12 '16 at 10:09

2 Answers2

3

You can't just use continue wherever, there is syntax to follow:

continue may only occur syntactically nested in a for or while loop, but not nested in a function or class definition or finally clause within that loop. It continues with the next cycle of the nearest enclosing loop.

Idos
  • 14,552
  • 14
  • 54
  • 70
0

As was mentioned, continue must be used within a loop. One way you could make your code work

while True:
    try:
        num1m = int(input("Select the first number --> "))
    except ValueError:
        print("That's no number!")
        continue
    try:
        num2m = int(input("Select the second number --> "))
    except ValueError:
        print("That's no number!")
        continue
    break
num3m = (num1m * num2m)
str(num3m)
print("The sum is " + str(num3m) + ".") # make sure to convert int to str
Michal Frystacky
  • 1,378
  • 21
  • 38