首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python循环w/异常处理永不结束

Python循环w/异常处理永不结束
EN

Stack Overflow用户
提问于 2019-10-07 20:24:35
回答 1查看 128关注 0票数 0

我遇到了一个我搞不懂的小问题。我被困在了一个时间循环里。我有3个while循环,第一个循环按计划执行,然后进入第二个循环。但是它被卡在了第二个,我不知道为什么。

对我想要做的事情做一个小小的解释:

我应该得到3个输入:经验年(年份)、性能(性能)和在1-10(级别)之间随机生成的int。该程序将询问用户的经验,如果它是3-11之间,他们是合格的。如果没有,它会告诉他们他们没有资格,并要求重新输入一个值.表演也是一样。如果他们输入一个小于或等于11的数字,它将生成随机整数(级别),在此点水平将用于评估他们的奖金。用户将得到提示以获得体验,并将正确地工作并继续执行。然而,即使输入一个有效的输入,它也不断要求他们重新输入性能#。我搞不懂它为什么会被困在这里。

代码语言:javascript
复制
import random
error = True
expError = True
performanceError = True
# Get users name
name = input("Enter your name: ")

# Get users experience *MINIMUM of 3 yrs py
while (expError):
    try:
        yearsexp = int (input(name+", Enter the years of your experience: "))
        if (yearsexp >= 3 and yearsexp <= 11):
            expError = False
            print(name, "You are qualified")
        else:
            raise ValueError
    except:
        print ("You have entered an invalid number! Try again...")

#Get users performance
while (performanceError):
    try:
        performance = int (input(name+", Enter the performance: "))
        if (performance <= 11):
            expError = False
            print(name, "You are qualified")
        else:
            raise ValueError
    except:
        print ("You have entered an invalid number! Try again...")
        performanceError = False


# Get random level number
level = random.randint(1,11)
print ("Random Level: ", end =' ')
print (level)
bonus = 5000.00

while (error):
    try:
        if (level >=5 and level <=8):
            error = False
            print ("Expected Bonus: $5,000.00")
            print (name + ", your bonus is $", end =' ')
            print (bonus)
        elif (level <= 4 ):
            error = False
            bonus = bonus * yearsexp * performance * level
            print ("Expected bonus: ", end =' ')
            print (bonus)
            print (name + ", your bonus is $", end =' ')
            print (bonus)
        else:
            raise ValueError
    except:
        print ("You do not get a bonus")
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-07 20:29:16

您没有将performanceError设置为False

代码语言:javascript
复制
if (performance <= 11):
    expError = False

需要更改为

代码语言:javascript
复制
if (performance <= 11):
    performanceError= False
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58276660

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档