添加循环变量值
Add Looped Variable Values
我正在参加 python 课程,有一个问题需要添加用户的意见。
问题是:
Sometimes a programmer does not know how many times data is to be
entered. For example, suppose you want to create a program that adds
an unspecified amount of positive numbers entered by the user. The
program stops adding numbers when the user enters a zero or a negative
number. Then the program prints the total. Before creating this
program, review the three actions required for all loops:
a. Initialize a variable that will be used in the test condition: What
will be tested to determine if the loop is executed or not? Write a
line of code that initializes a variable to be used in the test
condition of the loop for this program. The variable should contain a
value entered by the user.
b. Include a test condition that causes the loop to end when the
condition is false: What is the test condition for the while loop used
in this program?
c. Within the loop body, update the variable used in the test
condition: Write the code for the loop body. Include the code to
update the variable in the test condition.
d. Complete the program. Enter and execute the code. Does it work
properly?
我的密码是:
while True:
a = int(input("Enter a positive number: "))
total = a + a
if a>0:
a+a
else:
print("The number you entered is a negative number or 0 \n The sum is: ", total)
break
程序没有添加用户输入的所有值,我该如何解决?
在计算中使用输入之前,您应该测试循环是否结束。然后你不跳出循环,将输入添加到total变量,而不是自身。
total = 0
while True:
a = int(input("Enter a positive number: "))
if a <= 0:
print("The number you entered is a negative number or 0 \n The sum is: ", total)
break
total += a
您的方向正确,但您的代码需要一些调整。
分解你的代码:
while True:
a = int(input("Enter a positive number: "))
到目前为止一切正常。您创建一个循环,获取用户输入,转换为整数值并保存在名为 a
的变量中
total = a+a
这是出现一些问题的地方。发生的事情是您将 a
和 a
加在一起,基本上是将用户的输入值加倍并将其保存为总数。
要保持 运行 总数,您需要将用户输入添加到 total
的任何值。
像这样:
total = total + a # longway
# or
total += a # shorthand way (preferable)
您还需要将 total += a
移动到您的条件语句中。
if a>0:
total+=0
else:
# ~~ Do Stuff ~~~
这样做将允许您的条件在将其添加到总数之前检查用户的输入是否为正。但是,您会注意到在条件内移动总计会导致 NameError: name 'total' is not defined error
.
要解决此问题,您需要在循环外声明 total
。如果您要在循环内声明 total
,则该值将在循环的每次迭代中重置。在 total += a
的情况下,您尝试将值添加到单元化变量。
您的最终代码如下所示:
# Initialize total
total = 0
# Get and sum user input
while True:
a = int(input("Enter a positive number: "))
if a > 0:
total += a
else:
print("The number you entered is a negative number or 0 \n The sum is: ", total)
break
为了稍微清理一下,您还可以在将用户输入添加到总数之前检查是否应该退出循环。
结果代码:
# Initialize total
total = 0
# Get and sum user input
while True:
a = int(input("Enter a positive number: "))
if a <= 0:
print("The number you entered is a negative number or 0 \n The sum is: ", total)
break
total += a
您可以进一步展开。例如,您可以将用户输入移动到一个函数中并在循环中调用该函数。您会注意到,如果您输入 non-integer 值,您的代码就会中断。您可以将代码添加到验证用户输入是否为整数的函数中。
结果代码:
# Initialize total
total = 0
# Function to get and validate input
def get_input():
while True:
try:
user_input = int(input("Enter a number: "))
return user_input
except ValueError:
print("That was not a number, try again.")
# Get and sum user input
while True:
a = get_input()
if a <= 0:
print("The number you entered is a negative number or 0 \n The sum is: ", total)
break
total += a
我正在参加 python 课程,有一个问题需要添加用户的意见。
问题是:
Sometimes a programmer does not know how many times data is to be entered. For example, suppose you want to create a program that adds an unspecified amount of positive numbers entered by the user. The program stops adding numbers when the user enters a zero or a negative number. Then the program prints the total. Before creating this program, review the three actions required for all loops:
a. Initialize a variable that will be used in the test condition: What will be tested to determine if the loop is executed or not? Write a line of code that initializes a variable to be used in the test condition of the loop for this program. The variable should contain a value entered by the user.
b. Include a test condition that causes the loop to end when the condition is false: What is the test condition for the while loop used in this program?
c. Within the loop body, update the variable used in the test condition: Write the code for the loop body. Include the code to update the variable in the test condition.
d. Complete the program. Enter and execute the code. Does it work properly?
我的密码是:
while True:
a = int(input("Enter a positive number: "))
total = a + a
if a>0:
a+a
else:
print("The number you entered is a negative number or 0 \n The sum is: ", total)
break
程序没有添加用户输入的所有值,我该如何解决?
在计算中使用输入之前,您应该测试循环是否结束。然后你不跳出循环,将输入添加到total变量,而不是自身。
total = 0
while True:
a = int(input("Enter a positive number: "))
if a <= 0:
print("The number you entered is a negative number or 0 \n The sum is: ", total)
break
total += a
您的方向正确,但您的代码需要一些调整。
分解你的代码:
while True:
a = int(input("Enter a positive number: "))
到目前为止一切正常。您创建一个循环,获取用户输入,转换为整数值并保存在名为 a
total = a+a
这是出现一些问题的地方。发生的事情是您将 a
和 a
加在一起,基本上是将用户的输入值加倍并将其保存为总数。
要保持 运行 总数,您需要将用户输入添加到 total
的任何值。
像这样:
total = total + a # longway
# or
total += a # shorthand way (preferable)
您还需要将 total += a
移动到您的条件语句中。
if a>0:
total+=0
else:
# ~~ Do Stuff ~~~
这样做将允许您的条件在将其添加到总数之前检查用户的输入是否为正。但是,您会注意到在条件内移动总计会导致 NameError: name 'total' is not defined error
.
要解决此问题,您需要在循环外声明 total
。如果您要在循环内声明 total
,则该值将在循环的每次迭代中重置。在 total += a
的情况下,您尝试将值添加到单元化变量。
您的最终代码如下所示:
# Initialize total
total = 0
# Get and sum user input
while True:
a = int(input("Enter a positive number: "))
if a > 0:
total += a
else:
print("The number you entered is a negative number or 0 \n The sum is: ", total)
break
为了稍微清理一下,您还可以在将用户输入添加到总数之前检查是否应该退出循环。
结果代码:
# Initialize total
total = 0
# Get and sum user input
while True:
a = int(input("Enter a positive number: "))
if a <= 0:
print("The number you entered is a negative number or 0 \n The sum is: ", total)
break
total += a
您可以进一步展开。例如,您可以将用户输入移动到一个函数中并在循环中调用该函数。您会注意到,如果您输入 non-integer 值,您的代码就会中断。您可以将代码添加到验证用户输入是否为整数的函数中。
结果代码:
# Initialize total
total = 0
# Function to get and validate input
def get_input():
while True:
try:
user_input = int(input("Enter a number: "))
return user_input
except ValueError:
print("That was not a number, try again.")
# Get and sum user input
while True:
a = get_input()
if a <= 0:
print("The number you entered is a negative number or 0 \n The sum is: ", total)
break
total += a