简体   繁体   English

在 python 中使用验证循环计算运行总数

[英]Calculating Running Total with a Validation Loop in python

I am new to python and trying to calculate a running total while validating the user input.我是 python 的新手,并试图在验证用户输入的同时计算运行总数。 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)

I can see that you have added too much unnecessary code and this is a very bad practice in python.我可以看到您添加了太多不必要的代码,这在 python 中是一个非常糟糕的做法。 I believe that this is the answer you are looking for: f"" = is called a formatted string and the while loop is unnecessary so after that, you should import sys and use the exit function under a while loop and exit under a for loop我相信这是您正在寻找的答案: f"" = 被称为格式化字符串,而 while 循环是不必要的,因此之后,您应该导入 sys 并在 while 循环下使用 exit 函数并在 for 循环下退出

from sys import exit as exit1


while True:
    max_value = int(input("Enter a value for the maximum number: "))
    if max_value<0:
        break
    number = int(input(f"Enter a number between 1 and {max_value} or negative number to end: "))
    total = max_value + number

    if number == 0 or number > max_value:
        print("Number entered is invalid!")
    else:
        print("Sum of all valid numbers you entered:", total)

So after reevaluating the code I wrote the first time I came up with the below and it functions as I would like.所以在重新评估我第一次写的代码之后,我想出了下面的代码,它可以按照我的意愿运行。 For a beginner that has only learned three chapters I think it is good.对于只学了三章的初学者来说,我觉得还是不错的。 Current knowledge base is Input, Processing, Output, Decision Structures, Boolean Logic, and Repetition Structures.当前的知识库是输入、处理、输出、决策结构、布尔逻辑和重复结构。

 # 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 number between 1 and max value.
 number = int(input("Enter a number between 1 and " + str(max_value) + " or negative 
 number to end: "))

 #Validate the user input is between 1 and max value
 # if so accumulate the number.
 while number >= 0:
     if number > 0 and number <= max_value:
        total = total + number

     elif number == 0 or number > max_value:
         print("Number entered is invalid!")

     number = int(input("Enter a number between 1 and " + str(max_value) + " or negative number to end: "))

 #Once user select a negative number, print total.
 else:
     print("Sum of all valid numbers you entered:", total)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM