如何获得连续的相同输入以中断循环?
How do I get consecutive same input to break loop?
我正在尝试制作一个学生学分计算器。当用户连续输入 0 两次时,它必须显示文本:You did have too many study breaks
。代码也应该到此结束。我什至不知道要开始解决这个问题,但我已经准备好了其余的代码。
def main():
months=int(input("Enter the number of months: "))
total=0
for i in range(months):
points=float(input("Enter the number of credits in month {}: ".format(i+1)))
total += points
average=total/months
if average >= 5:
print(f"You are a full time student and your monthly credit point average is {average:.1f}")
elif average < 5:
print(f"Your monthly credit point average {average:.1f} does not classify you as a full time student.")
if __name__ == "__main__":
main()
所以澄清一下,如果用户连续两次输入 0 点,循环应该中断并显示文本。提前谢谢你。
只需跟踪之前的输入:
prev = -1
for i in range(months):
points=float(input("Enter the number of credits in month {}: ".format(i+1)))
if not (prev or points):
break
total += points
prev = points
您可以使用变量来跟踪上次输入的先前值。然后检查这个“prev”变量并在每次迭代时与“current”变量进行比较。在获得新输入之前设置此“prev”值很重要。
如果两者相等,则说明用户连续输入了相同的内容。
示例:
def main():
months=int(input("Enter the number of months: "))
total=0
prev=0
data = 0
for i in range(months):
prev = data
data = input("Enter the number of credits in month {}: ".format(i+1))
if ((prev==data) && data == 0):
print("You have too many study breaks")
break
points=float(data)
total += points
average=total/months
if average >= 5:
print(f"You are a full time student and your monthly credit point average is {average:.1f}")
elif average < 5:
print(f"Your monthly credit point average {average:.1f} does not classify you as a full time student.")
if __name__ == "__main__":
main()
这里是代码,如果需要的话函数已经在里面了:
def main():
pointsEnd = 0
months=int(input("Enter the number of months: "))
total=0
for i in range(months):
points=float(input("Enter the number of credits in month {}: ".format(i+1)))
if points == 0: #here is the new code
pointsEnd += 1
if pointsEnd == 2:
end = print('You did have too many study breaks')
return end
()
total += points
average=total/months
if average >= 5:
print(f"You are a full time student and your monthly credit point average is {average:.1f}")
elif average < 5:
print(f"Your monthly credit point average {average:.1f} does not classify you as a full time student.")
if __name__ == "__main__":
main()
您可以创建一个新变量来计算重复项,并添加一个“if 语句”,其中 if 语句以“break”结尾
def main():
//counter
duplicates = 0
months=int(input("Enter the number of months: "))
total=0
for i in range(months):
points=float(input("Enter the number of credits in month {}: ".format(i+1)))
//Add checks if it is a duplicate, if there are 2 or more duplicate, end
//for sentence
if points == 0:
duplicates += 1
else:
duplicates = 0
if duplicates >= 2:
break
total += points
average=total/months
if average >= 5:
print(f"You are a full time student and your monthly credit point average is {average:.1f}")
elif average < 5:
print(f"Your monthly credit point average {average:.1f} does not classify you as a full time student.")
if __name__ == "__main__":
main()
我正在尝试制作一个学生学分计算器。当用户连续输入 0 两次时,它必须显示文本:You did have too many study breaks
。代码也应该到此结束。我什至不知道要开始解决这个问题,但我已经准备好了其余的代码。
def main():
months=int(input("Enter the number of months: "))
total=0
for i in range(months):
points=float(input("Enter the number of credits in month {}: ".format(i+1)))
total += points
average=total/months
if average >= 5:
print(f"You are a full time student and your monthly credit point average is {average:.1f}")
elif average < 5:
print(f"Your monthly credit point average {average:.1f} does not classify you as a full time student.")
if __name__ == "__main__":
main()
所以澄清一下,如果用户连续两次输入 0 点,循环应该中断并显示文本。提前谢谢你。
只需跟踪之前的输入:
prev = -1
for i in range(months):
points=float(input("Enter the number of credits in month {}: ".format(i+1)))
if not (prev or points):
break
total += points
prev = points
您可以使用变量来跟踪上次输入的先前值。然后检查这个“prev”变量并在每次迭代时与“current”变量进行比较。在获得新输入之前设置此“prev”值很重要。
如果两者相等,则说明用户连续输入了相同的内容。
示例:
def main():
months=int(input("Enter the number of months: "))
total=0
prev=0
data = 0
for i in range(months):
prev = data
data = input("Enter the number of credits in month {}: ".format(i+1))
if ((prev==data) && data == 0):
print("You have too many study breaks")
break
points=float(data)
total += points
average=total/months
if average >= 5:
print(f"You are a full time student and your monthly credit point average is {average:.1f}")
elif average < 5:
print(f"Your monthly credit point average {average:.1f} does not classify you as a full time student.")
if __name__ == "__main__":
main()
这里是代码,如果需要的话函数已经在里面了:
def main():
pointsEnd = 0
months=int(input("Enter the number of months: "))
total=0
for i in range(months):
points=float(input("Enter the number of credits in month {}: ".format(i+1)))
if points == 0: #here is the new code
pointsEnd += 1
if pointsEnd == 2:
end = print('You did have too many study breaks')
return end
()
total += points
average=total/months
if average >= 5:
print(f"You are a full time student and your monthly credit point average is {average:.1f}")
elif average < 5:
print(f"Your monthly credit point average {average:.1f} does not classify you as a full time student.")
if __name__ == "__main__":
main()
您可以创建一个新变量来计算重复项,并添加一个“if 语句”,其中 if 语句以“break”结尾
def main():
//counter
duplicates = 0
months=int(input("Enter the number of months: "))
total=0
for i in range(months):
points=float(input("Enter the number of credits in month {}: ".format(i+1)))
//Add checks if it is a duplicate, if there are 2 or more duplicate, end
//for sentence
if points == 0:
duplicates += 1
else:
duplicates = 0
if duplicates >= 2:
break
total += points
average=total/months
if average >= 5:
print(f"You are a full time student and your monthly credit point average is {average:.1f}")
elif average < 5:
print(f"Your monthly credit point average {average:.1f} does not classify you as a full time student.")
if __name__ == "__main__":
main()