Python Forum
Sum up numbers using while(strings)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sum up numbers using while(strings)
#1
a = input()
p = 0
contador = 0
while a != ".":
    a = input()
    contador = contador + 1
if a == ".":
    print(contador)
    print(p + a)
I need to write a program who reads number line by line until ".". It should stop reading at "." and print how many numbers i wrote(contador) and the sum between the numbers. I'm getting this error:
Error:
Traceback (most recent call last): File "C:/Users/Utilizador/AppData/Local/Programs/Python/Python36-32/dffg.py", line 9, in <module> print(p + a) TypeError: unsupported operand type(s) for +: 'int' and 'str'
Reply
#2
p and a are of different types. a is returned from the value of input() which always returns a string, and p is defaulted to an int. You cant make a math op from a string and an int.
Recommended Tutorials:
Reply
#3
But i need a string in this case "." to stop the program and print the sum and count of numbers. How do i solve this then?
Reply
#4
First, why do you need to print the "."? Second, what is the purpose of "p"?
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#5
My teacher said the program had to read numbers line by line until the user writes a stop("."). Then it should print how many numbers you printed(contador) and the sum of all the numbers. p = 0 is a cumulative to have the sum in the end
Reply
#6
I don't read that as a requirement to print the ".", if you feel you should, modify your 'print' statement. As to the 'p', where are you modifying its value, as you have it now, 'p' will always equal 0.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#7
Can you make a suggestion then please?
Reply
#8
Just as you have a counter for 'contador', you also need to add to 'p' the values entered for 'a'.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Random Generator: From Word to Numbers, from Numbers to n possibles Words Yamiyozx 2 1,462 Jan-02-2023, 05:08 PM
Last Post: deanhystad
  Convert list of numbers to string of numbers kam_uk 5 3,055 Nov-21-2020, 03:10 PM
Last Post: deanhystad
  In input, I want just numbers and not strings! aquerci 2 3,273 Oct-19-2018, 12:53 PM
Last Post: DeaD_EyE
  Regular Expressions in Files (find all phone numbers and credit card numbers) Amirsalar 2 4,133 Dec-05-2017, 09:48 AM
Last Post: DeaD_EyE
  Strings inside other strings - substrings OmarSinno 2 3,696 Oct-06-2017, 09:58 AM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020