【发布时间】:2015-11-08 20:22:38
【问题描述】:
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()
【问题讨论】:
-
寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定问题或错误和在问题本身中重现它所需的最短代码。没有明确的问题陈述的问题对其他读者没有用处。请参阅:How to create a Minimal, Complete, and Verifiable Example。
-
问题是什么?如果你在让它发挥作用时遇到问题,你必须更具体
-
另请注意,Stack Overflow 既不是论坛也不是教程或代码编写服务。这是一个问答网站,特定 编程问题(通常但不总是,包括一些代码)可以获得特定 答案。请使用tour 并仔细阅读help center 以了解有关该网站的更多信息,包括what is on-topic 和what is not、如何使用ask a good question,以及该网站的不同方面一般如何运作。跨度>
-
抱歉,我正在尝试让我的号码验证正常工作,因此拒绝了字符串,并允许用户“重试”。
-
当用户输入非数字字符时,会要求用户“再试一次”。正确输入后,它不会将其更改为浮点数,因此程序会出错。
标签: python python-3.x