python - Loop that adds user inputted numbers and breaks when user types "end" -
create function expects no arguments. function asks user enter series of numbers greater or equal zero, 1 @ time. user types end indicate there no more numbers. function computes sum of values entered.
i'm supposed able while loops , if statements.
the main issue i'm running don't know how make "end" stops loop, rather add value.
def sumfunction(): """sum of values entered""" number = 0 while number >= 0 , number not "end": number = number + (float(input("enter next number: "))) return number
think loop checking for:
"do this, while number greater or equal 0 , not 'end'`.
which value satisfies both conditions? if enter 'end'
satisfy both criteria; because loop break if both conditions true.
so want or
not and
.
once solve issue, you'll run problem:
>>> def sum_function(): ... number = 0 ... while number >= 0 or number not 'end': ... number = number + float(input('enter number: ')) ... return number ... >>> sum_function() enter number: 1 enter number: 3 enter number: end traceback (most recent call last): file "<stdin>", line 1, in <module> file "<stdin>", line 4, in sum_function valueerror: not convert string float: end
the problem here converting float after entered; enters 'end'
, program crashes because cannot convert 'end'
float.
try solve 1 :-)
Comments
Post a Comment