我正在开展一个学校项目,但我遇到了问题。我必须为药剂师编写代码,客户可以在那里购买药品。因此,我需要制定限制,其中一个不与其他人一起使用等。以下是代码:
def prodajLek():
lekovi = Fajl1.UcitavanjeLekova()
lekoviRed = []
brojacZaForPetlju = 1
n = 0
cena = 0
kolicina = []
korpa = []
rednibrojevilekova = []
ukupnacena = 0
print(" Fabricki naziv Genericki naziv Serijski broj Kolicina Cena \n")
for i in lekovi:
x = i.strip().split("|")
lekoviRed.append(x)
if lekoviRed[n][5] == "False":
print(brojacZaForPetlju,"\t {:10} \t {:10} \t\t\t {:3} \t\t\t {:4} \t\t {:5}".format(x[0],x[1],x[2],x[3],x[4]))
brojacZaForPetlju = brojacZaForPetlju + 1
n = n + 1
print("\n\n\n\n")
rednibrleka = input("Izaberite redni broj leka koji zelite da prodate:\n>>\t")
rednibrleka = int(rednibrleka)
rednibrleka = rednibrleka - 1
rednibrojevilekova.append(rednibrleka)
kolicinaZahteva = input("Koju kolicinu zelite da prodate?\n>>\t")
kolicinaZahteva = int(kolicinaZahteva)
if kolicinaZahteva > int(lekoviRed[rednibrleka][3]):
print("Nema toliko na lageru!\n")
Fajl1.LekarMenu()
kolicina.append(kolicinaZahteva)
cena = int(lekoviRed[rednibrleka][4])
korpa.append(cena)
print("Da li zelite da kupite jos lekova?\n1.Da\n2.Ne\n")
nastavakKupovine = input(">>")
if nastavakKupovine == "1":
prodajLek()
elif nastavakKupovine == "2":
Fajl1.LekarMenu()
所以,当我进入nastavakKupovine输入时,当我按下1时,我需要继续购物并存储我的行号,我的价格和数量在数组rednibrojlekova = [],korpa = []和kolicina = []。但我有一个问题,因为我不知道如何在不重置这些数组的情况下继续这样做。
答案 0 :(得分:1)
您想要做的标准习惯是while True
循环。而不是展示如何改变你的(相当长的)功能,这是一个非常简单的功能,希望以一种简单的方式显示原理:
def ask():
answers = []
while True:
response = input("What do you have to say? ")
answers.append(response)
check = input("Type 'q' to quit, anything else to repeat: ")
if check == "q":
break
else:
continue
return answers
对于这个简单的功能,else: continue
部分是不必要的,因为无论如何循环都会继续,但是我已经将它包含在内以便您可以看到如何使用它。
以下是该行动中的一个功能示例:
>>> ask()
What do you have to say? Something
Type 'q' to quit, anything else to repeat:
What do you have to say? Another thing
Type 'q' to quit, anything else to repeat:
What do you have to say? Ok, done
Type 'q' to quit, anything else to repeat: q
['Something', 'Another thing', 'Ok, done']
>>>
通过阅读官方Python教程的More Control Flow Tools章节,您可以了解有关while
,break
和continue
的更多信息。