In this particular code I have to find the sum of even and odd number given the by user in stream, as I can observe everything is ok in the code but still not able to run it and getting ValueError: invalid literal for int() with base 10
:
sum_of_even_number=0
sum_of_odd_number=0
while True: # We have created a infinite loop
user_input=input()
if user_input=="\n\n":
break
num=int(user_input)
if num % 2==0:
sum_of_even_number =num
else:
sum_of_odd_number =num
print("sum_of_even_number : ",sum_of_even_number)
print("sum_of_odd_number : ",sum_of_odd_number)
CodePudding user response:
You're getting a Value Error because you are trying to parse an empty string. If the user presses the enter key, then it gets formatted as a string with no text. So instead of checking to see if your text contains \n
, check to see if its an empty string or not, like this:
sum_of_even_number=0
sum_of_odd_number=0
while True: # We have created a infinite loop
user_input=input()
if user_input == "":
break
num = int(user_input)
if num % 2 == 0:
sum_of_even_number = num
else:
sum_of_odd_number =num
print("sum_of_even_number : ",sum_of_even_number)
print("sum_of_odd_number : ",sum_of_odd_number)
For more help: ValueError: invalid literal for int() with base 10: ''
CodePudding user response:
Your while loop can be controlled by user input. In this example, if user input is empty then the loop will break. Try this:
evens_and_odds = [0, 0]
while user_input := input('Please enter a number: '):
try:
x = int(user_input)
evens_and_odds[x%2] = x
except ValueError as e:
print(e)
for i, e in enumerate(('even', 'odd')):
print(f'Sum of {e}s = {evens_and_odds[i]}')
Note:
Always test user input when you expect numbers.
You will need Python 3.8 for this
CodePudding user response:
See comments in line.
sum_of_even_number = 0
sum_of_odd_number = 0
while True: # We have created a infinite loop
user_input = input()
# Change if user_input=="\n\n": to if user_input == "":
# input() does not return \n
# If no entry is made input() returns an empty string
if user_input == "":
break
# The original code passed empty string.
# The following produced the value error because the empty string can not
# be converted to integer.
num = int(user_input)
if num % 2 == 0:
sum_of_even_number = num
else:
sum_of_odd_number = num
CodePudding user response:
Edit: Fixing indentation.
Without Walrus operator:
Before: Using same code as your. I put print
inside if/else
condition block Use the f-string
format as well.
sum_of_even_number=0
sum_of_odd_number=0
while True: # We have created a infinite loop
user_input=input('Enter number:')
if user_input=="\n\n":
break
num=int(user_input)
if num % 2==0:
sum_of_even_number =num
print(f"sum_of_even_number : {sum_of_even_number}")
else:
sum_of_odd_number =num
print(f"sum_of_odd_number : {sum_of_odd_number}")
With Python 3.8 Walrus Operator:
sum_of_even_number=0
sum_of_odd_number=0
while (user_input := input('Enter number:')): # We have created a infinite loop
num = int(user_input)
if num % 2 == 0:
sum_of_even_number = num
print(f"sum_of_even_number : {sum_of_even_number}")
else:
sum_of_odd_number = num
print(f"sum_of_odd_number : {sum_of_odd_number}")
Both output are same.
Enter number:6
sum_of_even_number : 6
Enter number:5
sum_of_odd_number : 5
Enter number:8
sum_of_even_number : 8
Enter number:100
sum_of_even_number : 100
Enter number:105
sum_of_odd_number : 105
Enter number: