Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 unexpected EOF while parsing
#1
On executing the script the python prompts for number until the user enters null as an input
def main():
        total = 0 
        number = input('Enter a number : ')
        while number != '':
                total += int(number)
                number = input('Enter a number : ')
        print('Sum of all input number is : ',total)
if __name__=='__main__':
        main()

Error
python sumNumbers.py 
Enter a number : 2
Enter a number : 2
Enter a number : 2
Enter a number : 
Traceback (most recent call last):
  File "sumNumbers.py", line 9, in <module>
    main()
  File "sumNumbers.py", line 6, in main
    number = input('Enter a number : ')
  File "<string>", line 0
    
    ^
SyntaxError: unexpected EOF while parsing
Quote
#2
You never break out of the loop.
In a function can use return as return always get value out and exit function.
indentation is 4-space not 8.
def main():
    total = 0
    number = input('Enter a number : ')
    while number != '':
        total += int(number)
        number = input('Enter a number : ')
    return f'Sum of all input number is : {total}'

if __name__=='__main__':
    print(main()) 
Quote
#3
Besides ... get a glance to Python powerful!
Output:
C:\Training>python somma.py Enter a number : 1234567890123456789012345678901234567890123456789012345678901234567890 Enter a number : 1 Enter a number : Sum of all input numbers is : 1234567890123456789012345678901234567890123456789012345678901234567891
Cheers
Quote
#4
thanks a lot snippsat
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  unexpected EOF while parsing aaron92 3 162 Sep-11-2019, 10:01 PM
Last Post: aaron92
  Python Script for parsing dictionary values from yaml file pawan6782 3 138 Sep-04-2019, 07:21 PM
Last Post: pawan6782
  Unexpected output: if statement CabbageMan 1 66 Sep-04-2019, 04:12 PM
Last Post: ThomasL
  string parsing question ( I think) diggee17 4 193 Jul-24-2019, 02:37 PM
Last Post: diggee17
  Unexpected Output using classes and inheritance langley 2 176 Jul-04-2019, 09:33 AM
Last Post: langley
  Parsing Soap XML response grootkarzijn 7 586 Jun-14-2019, 01:30 AM
Last Post: snippsat
  Parsing an MBOX file Oliver 1 550 May-26-2019, 07:12 AM
Last Post: heiner55
  XML Parsing Child karthi_python 1 191 May-16-2019, 01:37 PM
Last Post: karthi_python
  Issues parsing the response from a request garnold 3 296 May-14-2019, 12:39 PM
Last Post: snippsat
  parsing logical expression with pyparsing palo173 2 384 May-13-2019, 09:22 AM
Last Post: palo173

Forum Jump:


Users browsing this thread: 1 Guest(s)