Python 在 While 循环中继续缩进
Python continue indentation in While loop
我正在编写一个程序,该程序具有多个选项来修改 python 中的字典。用户有四个选项,完成一个选项后我希望程序将用户带回主菜单。
到目前为止,每个选项都正常工作,除了它不会将用户带回主菜单,而是永远循环
user_input = int(input("Faites un choix..."))
liste_epicerie = {}
while True:
if user_input == 1:
print(liste_epicerie)
if liste_epicerie == {}:
print("La liste est vide")
continue
所以这段代码应该将用户带回到 user_input,而是永远打印 "La liste est vide"。
我做错了什么?
您必须再次实际读取用户输入(在循环内):
liste_epicerie = {}
while True:
user_input = int(input("Faites un choix..."))
if user_input == 1:
# ...
elif ...:
# ...
# under some condition
break
变量user_input
不会神奇地记住并重复它的值是如何产生的。
sum=0
count=0
while True:
n=input ("enter the value :")
if n=="exit":
break
try:
float(n)
except:
print ("enter the numeric value")
continue
sum=sum+n
count=count+1
print(sum,count,sum/count)
我正在编写一个程序,该程序具有多个选项来修改 python 中的字典。用户有四个选项,完成一个选项后我希望程序将用户带回主菜单。 到目前为止,每个选项都正常工作,除了它不会将用户带回主菜单,而是永远循环
user_input = int(input("Faites un choix..."))
liste_epicerie = {}
while True:
if user_input == 1:
print(liste_epicerie)
if liste_epicerie == {}:
print("La liste est vide")
continue
所以这段代码应该将用户带回到 user_input,而是永远打印 "La liste est vide"。 我做错了什么?
您必须再次实际读取用户输入(在循环内):
liste_epicerie = {}
while True:
user_input = int(input("Faites un choix..."))
if user_input == 1:
# ...
elif ...:
# ...
# under some condition
break
变量user_input
不会神奇地记住并重复它的值是如何产生的。
sum=0
count=0
while True:
n=input ("enter the value :")
if n=="exit":
break
try:
float(n)
except:
print ("enter the numeric value")
continue
sum=sum+n
count=count+1
print(sum,count,sum/count)