[英]in range loop skipping through
salary=0
salaryArray=[]
loop=0
noYears=int(input("How many years do you want to do salaries for? "))
for i in range(0,noYears):
while loop==0:
print()
print("You can add multiple sources of income, one at a time")
salaryType=input("Do you want to put in your salary hourly or yearly? (h/y) ")
if salaryType=="y":
salarySection=float(input("What is your salary? "))
salary=salarySection+salary
else:
salaryHourly=float(input("What are you payed per hour? "))
salaryWeekly=float(input("How many hours per week will you work? "))
salaryYearly=float(input("How many weeks per year will you work? "))
print()
salarySection=salaryHourly*salaryWeekly*salaryYearly
salary=salary+salarySection
repeat=input("Do you wish to add another source of income? (y/n) ")
if repeat=="n":
print("This year's anual salary is", salary)
salaryArray.append(salary)
loop=1
出於某種原因for i in range(0,noYears)
不起作用? 它只是在完成一次后移動到下一行代碼 - 即使我將 noYears 的答案設為 3。
任何人都知道為什么這可能是因為我看不出有什么問題? :)
代碼不起作用,因為 while 循環永遠不會執行。 你可以通過兩種方式解決這個問題。
使用 break 語句而不是將循環設置為 1:
#previous code repeat=input("Do you wish to add another source of income? (y/n) ") if repeat=="n": print("This year's anual salary is", salary) salaryArray.append(salary) break
在 for 循環內將變量循環重置為 0:
for i in range(0,noYears): loop = 0 while loop==0: # remaining code
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.