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