salary=0salaryArray=[]loop=0noYears=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。任何人都知道为什么会这样,因为我看不出出了什么问题?:)
1 回答
子衿沉夜
TA贡献1828条经验 获得超3个赞
该代码不起作用,因为 while 循环从未执行。你可以通过两种方式解决这个问题。
使用break语句而不是将loop设置为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
添加回答
举报
0/150
提交
取消