基本上,我试图让我的程序在输入screen_number后循环回main_screen,无限重复,直到我按T终止它。同时,P应该跟踪我在整个会话中输入了多少个数字。这是我目前所知道的,我只知道如何循环screen_number。只是不知道如何循环回main_screen或者如何创建"函数。
main_screen = str(input("Pick W to add a new number, P to show how many numbers, high or low, have been added, or T to terminate: "))
if(main_screen == "W"):
screen_number = int(input("Input your grade: "))
if(screen_number >= 10):
print("It's high.")
elif(user_grade < 10):
print("It's low.")
if(user_menu == "T"):
print("Terminating system.")
else:
print("Invalid.")```
试试这个:
while True:
main_screen = input("Pick W to add a new numbernP to show how many numbers high or low have been addednT to terminate: ")
if main_screen == "W":
user_grade = int(input("Input your grade: "))
if user_grade >= 10:
print("It's high.")
elif user_grade < 10:
print("It's low.")
elif main_screen == "T":
print("Terminating system.")
break
else:
print("Invalid.")
尝试使用条件进行循环,并在输入正确的输入时中断:
while True:
main_screen = str(input("Pick W to add a new number, P to show how many numbers,
high or low, have been added, or T to terminate: "))
if(main_screen == "W"):
screen_number = int(input("Input your grade: "))
if(screen_number >= 10):
print("It's high.")
elif(user_grade < 10):
print("It's low.")
if(user_menu == "T"):
print("Terminating system.")
return
else:
print("Invalid.")
创建一个列表,并在其中存储"P"的值。使用while
循环返回main_screen
l = []
while True:
main_screen = input("Pick W to add a new number, P to show how many numbers, high or low, have been added, or T to terminate: "))
if (main_screen == "W"):
screen_number = int(input("Input your grade: "))
if (screen_number >= 10):
print("It's high.")
elif (user_grade < 10):
print("It's low.")
l.append(screen_number)
elif (user_menu == "T"):
print("Terminating system.")
break
elif (user_menu == "P"):
print(f"No. of numbers is {len(l)}")
else:
print("Invalid.")