• 0
Votes
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 195

Backtrace:

File: /home/u125378470/domains/lawhelpguru.org/public_html/application/views/question.php
Line: 195
Function: _error_handler

File: /home/u125378470/domains/lawhelpguru.org/public_html/application/controllers/Questions.php
Line: 416
Function: view

File: /home/u125378470/domains/lawhelpguru.org/public_html/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Calculating Running Total with a Validation Loop in python

<button aria-describedby="--stacks-s-tooltip-yt7fvvrk" aria-label="Bookmark" aria-pressed="false" class="js-bookmark-btn s-btn s-btn__unset c-pointer py4 js-gps-track" data-controller="s-tooltip" data-gps-track="post.click({ item: 1, priv: 0, post_type: 1 })" data-s-tooltip-placement="right" data-selected-classes="fc-yellow-600"></button><svg aria-hidden="true" class="mln2 mr0 svg-icon iconHistory" height="18" viewbox="0 0 19 18" width="19"></svg>

 

I am new to python and trying to calculate a running total while validating the user input. If the user inputs invalid data I am showing an error and asking the user to input the correct data. My issue is I am unable to determine how to calculate only the valid data input from the user instead of all values.

# This program calculates the sum of a series
# of numbers entered by the user.

#Initialize an accumulator variable.
total = 0

#Request max value from user.
max_value = int(input("Enter a value for the maximum number: "))

#Request user to enter value between 1 and max value.
number = int(input("Enter a number between 1 and " + str(max_value) + " or negative number to end: "))

#Calculate total of user input values.
while number > 0:
      total = total + number
      number = int(input("Enter a number between 1 and " + str(max_value) + " or negative number to end:"))

#Display error if user entered invalid value.
    if number == 0 or number > max_value:
        print("Number entered is invalid!")

#Print sum of valid user input data. 
else:
    print("Sum of all valid numbers you entered:", total)