[英]Repeating a while loop after user input
I needed to create a program that sums the even numbers between 2 and 100. I have the part for this written:我需要创建一个程序,将 2 到 100 之间的偶数相加。我有这个部分:
def main():
num = 2
total = 0
while num <= 100:
if num % 2 == 0:
total = total + num
num += 1
else:
num += 1
print("Sum of even numbers between 2 and 100 is:", total)
main()
The next part is I'm supposed to add a while loop within the current while loop that asks for an input of Y/N and will repeat the program if the input is yes.下一部分是我应该在当前的 while 循环中添加一个 while 循环,该循环要求输入 Y/N,如果输入为是,将重复该程序。 I have spent a long time trying to put the following in different locations:
我花了很长时间尝试将以下内容放在不同的位置:
while again == "Y" or again == "y":
again = input("Would you like to run this again? (Y/N)")
But I haven't been able to get it to work or in best case I get it to print the total of the numbers and ask if I want to run it again but when I type yes it goes back to asking if I want to run it again.但是我一直无法让它工作,或者在最好的情况下我让它打印数字的总数并询问我是否想再次运行它但是当我输入 yes 时它又回到询问我是否想运行再说一遍。
Where do I put the other while statement?我把另一个 while 语句放在哪里?
def sum_evens():
active = True
while active:
sum_of_evens = 0
for even in range(2, 101, 2):
sum_of_evens += even
print('Sum of the evens is:', sum_of_evens)
while True:
prompt = input('Type "Y" to start again or "N" to quit...\n')
if prompt.lower() == 'y': # doesn't matter if input is caps or not
break
elif prompt.lower() == 'n':
active = False
break
else:
print('You must input a valid option!')
continue
sum_evens()
If the while loop asking for running the program again does not have to be inside the loop computing the sum, then the following should answer your constraints:如果要求再次运行程序的 while 循环不必在计算总和的循环内,那么以下内容应该回答您的约束:
def main():
again = 'y'
while again.lower() == 'y':
num = 2
total = 0
while num <= 100:
if num % 2 == 0:
total = total + num
num += 1
else:
num += 1
print("Sum of even numbers between 2 and 100 is:", total)
again = input("Do you want to run this program again[Y/n]?")
main()
Do note that if you answer N
(no, or whatever else that is not Y
or y
) the program stops.请注意,如果您回答
N
(否,或其他非Y
或y
答案),程序将停止。 It does not ask forever.它不会永远要求。
It is always good to check if your summation has a closed form.检查您的求和是否有封闭形式总是好的。 This series being similar to thesum of positive integers , it has one so there is no reason to iterate over even numbers.
这个系列类似于正整数之和,它有一个,所以没有理由迭代偶数。
def sum_even(stop):
return (stop // 2) * (stop // 2 + 1)
print(sum_even(100)) # 2550
To put that in a while loop, ask for the user input after the function call and break if it is 'y'
.要将其放入 while 循环中,请在函数调用后询问用户输入,如果是
'y'
则中断。
while True:
stop = int(input('Sum even up to... '))
print(sum_even(stop))
if input('Type Y/y to run again? ').lower() != 'y':
break
Sum even up to... 100
2550
Type Y/y to run again? y
Sum even up to... 50
650
Type Y/y to run again? n
You should add the while loop at the beginning and ask the user at the end.您应该在开头添加 while 循环并在最后询问用户。 Like this:
像这样:
num = 2
total = 0
con_1 = True
while con_1:
while num <= 100:
if num % 2 == 0:
total = total + num
num += 1
else:
num += 1
print("Sum of even numbers between 2 and 100 is:", total)
ask = str(input("Do you want to run this program again(Y/n)?:"))
if ask == "Y" or ask == "y":
con_1 = True
elif ask == "N" or ask == "n":
con_1 = False
else:
print("Your input is out of bounds.")
break
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.