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 :).

3
  • 1
    Put the homeSide = ... inside the try-except. Commented Feb 12, 2021 at 13:58
  • You are already handling the ValueError when asking for the "away side", so just do the same for the "home side". Commented Feb 12, 2021 at 13:58
  • Related: stackoverflow.com/questions/23294658 Commented Feb 12, 2021 at 14:00

4 Answers 4

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.
Sign up to request clarification or add additional context in comments.

1 Comment

Better change that to while homeSide is None:, otherwise 0 will keep you in the loop.
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")

Comments

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.

Comments

0

This is the basic idea!

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.