0
from colorama import init, Fore, Style
import os

if Name == "Germany" or Name == "Italy" or Name == "Poland": 
        Height = int(input("How high should the image be? Maximum is 4608 pixels."))
        if Height < 1:
            print(Fore.RED + "This is not a valid height!")
            exit()
        elif height > 0:
            Width = int(Height * (16 / 9))

The following problem:

So far the program can already determine if the input is below 1, which would be an invalid height for an image. But how do I do that if I want to prevent that when a letter or something else is entered that such an ugly error message appears and the program instead

print(Fore.RED + "This is not a valid height!")
exit()

executes

If some more code is needed to understand the context, please let me know

ZygD
  • 22,092
  • 39
  • 79
  • 102
  • 4
    Iā€™m voting to close this question because the question is not in English. ā€“ Thomas Sablik Dec 27 '20 at 14:52
  • 1
    https://stackoverflow.com/questions/8075877/converting-string-to-int-using-try-except-in-python ā€“ Thomas Sablik Dec 27 '20 at 14:55
  • 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) ā€“ Tomerikoo Dec 29 '20 at 07:50

1 Answers1

0

You can easily use Exception, except ValueError and TypeError

Code Syntax

try:
    Height = int(input("How high should the image be? Maximum is 4608 pixels."))
        if Height < 1:
            print(Fore.RED + "This is not a valid height!")
            exit()
        elif height > 0:
            Width = int(Height * (16 / 9))
except (TypeError, ValueError):
    print(Fore.RED + "This is not a valid height!")
    exit()
Ahmed
  • 796
  • 1
  • 5
  • 16