Four things:
- Not sure why you storing the input as
float,intshould suffice. - If you give a negative input, it will enter the
whileloop. However, in thewhileloop, you are not actually assigning the new input touser_input. Fix this by addinguser_input = - The
whileloop guaranteesuser_inputis >= 0, soif user_input > 0:is unnecessary. - Probably the most important, to calculate the sum of digits, you need to repeatedly divide and sum, not just do it once. So, add a
whileloop.
Final code:
user_input = int(input("Please Enter Your Number: "))
s = 0
while user_input < 0:
user_input = int(input("Please Enter Another Number: "))
while user_input:
s += user_input % 10
user_input //= 10
print("You've entered a positive number! The sum of the digits is: ", s)
Answer from iz_ on Stack OverflowFinding Sum of Variables in Python - Stack Overflow
How would I make a variable that has the sum of two other variables in python?
python - Adding multiple variables to get the sum? - Stack Overflow
How do you make two variables add together in Python? - Stack Overflow
Four things:
- Not sure why you storing the input as
float,intshould suffice. - If you give a negative input, it will enter the
whileloop. However, in thewhileloop, you are not actually assigning the new input touser_input. Fix this by addinguser_input = - The
whileloop guaranteesuser_inputis >= 0, soif user_input > 0:is unnecessary. - Probably the most important, to calculate the sum of digits, you need to repeatedly divide and sum, not just do it once. So, add a
whileloop.
Final code:
user_input = int(input("Please Enter Your Number: "))
s = 0
while user_input < 0:
user_input = int(input("Please Enter Another Number: "))
while user_input:
s += user_input % 10
user_input //= 10
print("You've entered a positive number! The sum of the digits is: ", s)
The if statement is generally used to decide if something should be done once.
If you want to keep going until user_input becomes zero, you'll need a while.
Also, I'm not entirely certain why you're storing the number as a float, especially when you make that from an int anyway. It may as well just be an int.
Additionally, you're loop to re-enter the value if it was negative doesn't actually assign the new value to the variable.
And you probably also want to outdent the print statement lest it be done on every iteration of the loop you're about to add.
Of course, some may suggest a more Pythonic way of summing the digits of a positive number is a simple:
sum([int(ch) for ch in str(x)])
That works just as well, without having to worry about explicit loops.
I'm only in my first week of my python class. How do I write code to calculate the sum of a sequence (s) with initial value (a), amount added at each step (d) and number of terms (n). So if a=1, d=2 and n=5, calculate and report the sum of 1+3+5+7+9. For the summation formula: S=N/2(2A + (N-1) * D)
A = 1
print("Enter the Value of n: ")
n = int(input())
A = 1
sum = 0
#This is how far I've gotten on PyCharm with working code and I cannot figure it out for the life of me
str values can be added to each other much like numbers. The + operator you use works fine, but concatenates values for strings. The result of raw_input is a string (str), so that's why you'd see '555' in stead of 15. To sum numbers, use int() to coerce the values to numbers before adding them up:
try:
a = int(raw_input('gimme a number'))
except ValueError as e
print 'that was not a number, son'
Make sure that your raw_input actually Is an int():
shape = raw_input("Is the plot a (q) quadrilateral or (t) triangle?")
if shape.lower() == "q":
try:
a = raw_input("What is the length in feet of side 'a'?")
b = raw_input("What is the length in feet of side 'b'?")
c = raw_input("What is the length in feet of side 'c'?")
d = raw_input("What is the length in feet of side 'd'?")
perimeter = int(a) + int(b) + int(c) + int(d)
except ErrorValue as e
print "Please make sure you enter numbers only."
elif shape.lower() == "t":
try:
a = raw_input("What is the length in feet of side 'a'?")
b = raw_input("What is the length in feet of side 'b'?")
c = raw_input("What is the length in feet of side '
perimeter = int(a) + int(b) + int(c)
except ErrorValue as e
print "Please make sure you enter numbers only."
else:
print "Please enter 'q' for quadrilateral or 't' for triangle."