0

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

Chr3lle
  • 13
  • 4
  • 1
    `while True` would be easier to read than `while loop` and then having to look for where you defined `loop`. It's a widely-used idiom. – pfabri Feb 05 '21 at 10:19
  • Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – SiHa Feb 05 '21 at 10:30
  • This question has nothing to do with Pythagoras. – SiHa Feb 05 '21 at 10:32
  • Please translate your `print` statements to English. They are integral to understanding your intended program logic. I translated them myself on this occasion but this was a one-time exception for you as a new contributor. – pfabri Feb 05 '21 at 10:34
  • SiHa it is, but in danish so it looks weird. It makes pythagorean triples :) pfabri yeah will make sure to do that next time :) – Chr3lle Feb 05 '21 at 10:40
  • It would be preferable to not only do it next time but to edit this question. This will help others understand the problem more easily and will also be of more value to them. Thanks. – pfabri Feb 05 '21 at 10:46
  • @Chr3lle Good job adding the translation! :) – pfabri Feb 05 '21 at 12:31

2 Answers2

0
loop = True
print("Velkommen til pythagoras tripler generatoren, hvis du vil stoppe maskinen skal du bare skrive stop")
print("Vælg et ulige tal over 1")
while loop:
    tal = input("Skiv her: ")

    try:
        number = int(tal)
    except Exception as e:
        print("Det skal være et tal")
        raise e // You should raise a Exception to let this program stops here.

    if int(number) % 2 == 0 or int(number) == 1: // You lack one ':'
        print("tallet skal være ulige og større end 1")

    else:
        udregning = int(number) ** 2
        udregning2 = int(udregning) - 1
        udregning3 = int(udregning2) / 2
        print("Her er dit ene tal,", int(udregning3))
        udregning4 = int(udregning3) + 1
        print("Her er dit andet tal,", int(udregning4))

    if str(tal) == "stop":
        break
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
0

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!

pfabri
  • 885
  • 1
  • 9
  • 25