列表和数字验证

2024-09-26 22:09:16 发布

您现在位置:Python中文网/ 问答频道 /正文

strnumsold = str()
numsold = int()
Flag = False
index = 0
totprice = 0.0
avgsold = 0.0
aboveavg = 0
belowavg = 0
strnumcheck = str()
numcheck = float()


print("Welcome to the Home Sales Calculator!")
print(" _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _")
strnumsold = input("\nHow many homes were sold in the past year? ")
while Flag == False:
    if strnumsold.isdigit():
        numsold = int(strnumsold)
        Flag = True
    else:
        strnumsold = input("That is not a valid number! Try Again!")
Flag = False
tothomes = []
print("\nWhat did the home sell for? (#'s Only) ")
print("**********************************************")
for index in range(numsold):
        tothomes.append(input("{}) $ ".format(index + 1)))
        strnumcheck = tothomes[index]
        if strnumcheck.isdigit():
            numcheck = float(strnumcheck)
            Flag = True
            tothomes[index] = numcheck
        else:
            tothomes[index] = input("That is not a valid number! Try Again! ")
        index = index + 1
        Flag = False
tothomes.sort()
print("**********************************************")
for homes in reversed(tothomes):
    print("   $", "%.2f"%homes)
avgsold = sum(tothomes) / numsold
print("\nThe Average Price of homes sold was: $", "%.2f"%avgsold)
print("The Highest home sold was: ", "%.2f"%max(tothomes))
print("The Lowest home sold was:  ", "%.2f"%min(tothomes))
aboveavg = [above for above in tothomes if above >avgsold]
belowavg = [below for below in tothomes if below <avgsold]
print("The number of homes sold ABOVE Average was: ", len(aboveavg))
print("The number of homes sold BELOW Average was: ", len(belowavg))

这是我第一个既有列表又有数字验证的程序。在

我已经贴出了我在上面的距离,并且已经多次改写了这篇文章。在

我的号码验证有效,但在提示“再试一次”后再次输入, 它不会完全遍历循环并将字符串更改为浮点。在

当数字输入正确时,它的工作原理与假设的一样。 以下是输入错误时发生的情况示例:

欢迎使用房屋销售计算器!在


^{pr2}$

Tags: infalseforinputindexflagprintwas
1条回答
网友
1楼 · 发布于 2024-09-26 22:09:16

天哪! 让它跑,这是世界上最小的东西!

在For循环中,我在If语句上添加了一个额外的While循环。

它仍然不会运行,我在我的If语句的Else中意识到,我不得不切换到homes[index]到strnumcheck。

我展示了我在下面所做的改变:

print("\nWhat did the home sell for? (#'s Only) ")
print("**********************************************")
for index in range(numsold):
    tothomes.append(input("{}) $ ".format(index + 1)))
    strnumcheck = tothomes[index]
    while Flag == False:                          <<<<<<<<<<<<<<<<<<<<<<!
        if strnumcheck.isdigit():
            numcheck = float(strnumcheck)
            Flag = True
            tothomes[index] = numcheck
        else:
            strnumcheck = input                   <<<<<<<<<<<<<<<<<<<<<<!
("That is not a valid number! Try Again! ")
    index = index + 1
    Flag = False

相关问题 更多 >

    热门问题