程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了为什么我输入收入后我的代码开始循环? (Python)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决为什么我输入收入后我的代码开始循环? (Python)?

开发过程中遇到为什么我输入收入后我的代码开始循环? (Python)的问题如何解决?下面主要结合日常开发的经验,给出你关于为什么我输入收入后我的代码开始循环? (Python)的解决方法建议,希望对你解决为什么我输入收入后我的代码开始循环? (Python)有所启发或帮助;

当我输入婚姻状况时,代码会像应有的那样询问收入,但是当我输入收入时,代码决定循环并再次询问我的婚姻状况。请帮帮我!

while people < 1 or people > 20:##SetTing up loop
    people = int(input('How many people would you like to process? '))##Reasking user if invalid number.
    
x = 0

while x < people:
    status = str(input('Are you marrIEd or single?'))##input users marital status
    while status != 'single' and status != 'marrIEd':##SetTing up loop
        status = str(input('Are you marrIEd or single?'))##Reasking if user is single or marrIEd   
    income = int(input('what is your income? '))

    if status == 'single' and income <= 50000:
        tax = income * .10
    elif status == 'single' and income > 50000 and income <= 100000:
        tax = 2500 + ((income-50000) * .12)
    elif status == 'single' and income > 100000:
        tax = 6000 + ((income-100000) * .15)
    elif status == 'marrIEd' and income <= 50000:
        tax = income * .05
    elif status == 'marrIEd' and income > 50000 and income <= 100000:
        tax = 2500 + ((income-50000) * .08)
    else:
        tax = 6000 + ((income-100000) * .10)


        print('Marital Status: ' + status)
        print('Income: $%.2f' %incomE)
        print('Tax: $%.2f' %taX)

        x = x + 1```

解决方法

最后 3 个打印语句x=x+1 在 else 块内。

您只需要在 3 个打印语句和增加 x 的行之前删除 1 个制表符空间

正确的代码如下:

while people < 1 or people > 20:##SetTing up loop
    people = int(input('How many people would you like to process? '))##Reasking user if invalid number.
    
x = 0

while x < people:
    status = str(input('Are you married or single?'))##Input users marital status
    while status != 'single' and status != 'married':##SetTing up loop
        status = str(input('Are you married or single?'))##Reasking if user is single or married   
    income = int(input('what is your income? '))

    if status == 'single' and income <= 50000:
        tax = income * .10
    elif status == 'single' and income > 50000 and income <= 100000:
        tax = 2500 + ((income-50000) * .12)
    elif status == 'single' and income > 100000:
        tax = 6000 + ((income-100000) * .15)
    elif status == 'married' and income <= 50000:
        tax = income * .05
    elif status == 'married' and income > 50000 and income <= 100000:
        tax = 2500 + ((income-50000) * .08)
    else:
        tax = 6000 + ((income-100000) * .10)


    print('Marital Status: ' + status)
    print('Income: $%.2f' %incomE)
    print('Tax: $%.2f' %taX)

    x = x + 1

这是一个简单的缩进错误。

大佬总结

以上是大佬教程为你收集整理的为什么我输入收入后我的代码开始循环? (Python)全部内容,希望文章能够帮你解决为什么我输入收入后我的代码开始循环? (Python)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:(Python)