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
  Parsing Soap XML response grootkarzijn 7 241 Jun-14-2019, 01:30 AM
Last Post: snippsat
  Parsing an MBOX file Oliver 1 254 May-26-2019, 07:12 AM
Last Post: heiner55
  XML Parsing Child karthi_python 1 115 May-16-2019, 01:37 PM
Last Post: karthi_python
  Issues parsing the response from a request garnold 3 194 May-14-2019, 12:39 PM
Last Post: snippsat
  parsing logical expression with pyparsing palo173 2 180 May-13-2019, 09:22 AM
Last Post: palo173
  syntax error near unexpected token btom529 5 267 Apr-21-2019, 09:03 PM
Last Post: btom529
  Parsing Attached .MSG Files with Python3 ericl42 1 175 Apr-12-2019, 06:28 PM
Last Post: ericl42
  parsing complex text file anna 1 183 Apr-10-2019, 09:54 PM
Last Post: Larz60+
  Getting an unexpected generator object ? mcgrim 19 424 Mar-29-2019, 11:35 AM
Last Post: gontajones
  Parsing Large Numbers Question amyvaulhausen 5 356 Mar-24-2019, 08:46 AM
Last Post: samsonite

Forum Jump:


Users browsing this thread: 1 Guest(s)