1

I simply try to get two inputs both of which should be integers and give an explanation related to their comparison. Meanwhile, I want to give them an error message when the input belongs to some other value. However, when I want to test the error message by entering a string input, I get this error instead of creating a loop to display the main question again:

Traceback (most recent call last):
  File "C:\Users\Green\PycharmProjects\pythonProject6\main.py", line 21, in <module>
    comparison()
  File "C:\Users\Green\PycharmProjects\pythonProject6\main.py", line 2, in comparison
    homeSide = int(input("Please enter how many goals did the home side score:"))
ValueError: invalid literal for int() with base 10: 'adasdasdsa'

As the code lines are short, I want to display it here as a whole:

def comparison():
    homeSide = int(input("Please enter how many goals did the home side score:"))

    while True:
        try:
            awaySide = int(input('Please enter how many goals did the away side score :'))
        except ValueError:
            print("Please enter a number!")
            continue
        break

        if homeSide > awaySide:
            print("Home side got 3 points.")

        elif homeSide == awaySide:
            print("Both sides got 1 point.")

        else:
            print("Home side could not get any point.")

comparison()

All in all, how can I overcome this problem. Thank you in advance for your help :).

Sreeram TP
  • 11,346
  • 7
  • 54
  • 108

4 Answers4

1

Add try and except for the first input.

Instead adding the input to loop and break it, can do something like this, which looks a bit neater,

def comparison():
    
    homeSide = None
    awaySide = None
    
    while homeSide is None:
        try:
            homeSide = int(input("Please enter how many goals did the home side score:"))
        except ValueError:
            print("Please enter a number")
            
    while awaySide is None:
        try:
            awaySide = int(input('Please enter how many goals did the away side score :'))
        except ValueError:
            print("Please enter a number!")
            

    if homeSide > awaySide:
        print("Home side got 3 points.")

    elif homeSide == awaySide:
        print("Both sides got 1 point.")

    else:
        print("Home side could not get any point.")

comparison()


Please enter how many goals did the home side score:i
Please enter a number
Please enter how many goals did the home side score:90
Please enter how many goals did the away side score :as
Please enter a number!
Please enter how many goals did the away side score :1
Home side got 3 points.
Sreeram TP
  • 11,346
  • 7
  • 54
  • 108
1

You can also validate the variables using: isInstance(int, variable)

Example:

homeSide = input("Please enter how many goals did the home side score:")

if not isInstance (int, homeSide):
   print(f"{homeside} is not a number. Please enter a number using 1-10")
Myotha
  • 66
  • 4
0
def comparison():
    isHome = False
    while True:
        try:
            if not isHome:
                homeSide = int(
                    input("Please enter how many goals did the home side score:"))
                isHome = True
            awaySide = int(
                input('Please enter how many goals did the away side score :'))
        except ValueError:
            print("Please enter a number!")
            continue

        if homeSide > awaySide:
            print("Home side got 3 points.")

        elif homeSide == awaySide:
            print("Both sides got 1 point.")

        else:
            print("Home side could not get any point.")
        break


comparison()

output:

Please enter how many goals did the home side score:234  
Please enter how many goals did the away side score :asdfasd
Please enter a number!
Please enter how many goals did the away side score :32
Home side got 3 points.
Lohith
  • 866
  • 1
  • 9
  • 25
0

This is the basic idea!

while True:
    try:
        x = int(input())
        break
    except ValueError:
        print("Error: invalid type!")