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))
这是我的第一个包含列表和数字验证的程序。
我已经发布了我在上面的距离,并且已经多次重写/试图纠正它。
我的号码验证有效,但在提示“再试一次”后再次输入时, 它不会完全通过循环并将字符串更改为浮点数。
正确输入数字时,它可以正常工作。 以下是给出错误输入时发生的事件的示例:
欢迎使用家庭销售计算器!
How many homes were sold in the past year? 3
What did the home sell for? (#'s Only)
**********************************************
1) $ 25
2) $ 36
3) $ m
That is not a valid number! Try Again! 24
Traceback (most recent call last):
File "C:\Users\troythomas448\Desktop\ITCS1140\Assignments\Thomas, Troy Lab#8.3.py", line 37, in <module>
tothomes.sort()
TypeError: unorderable types: str() < float()
答案 0 :(得分:0)
圣牛! 得到了它,它是世界上最小的东西!
在我的For循环中,我在If语句中添加了一个额外的While循环。
它仍然无法运行,我在我的Else中意识到我的If语句,我不得不将tothomes [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