新手:因为我在不递增的范围内



我试图为课堂做一项作业,并使其变得比需要的更难,但我这样做是为了学习。

有人能看到我的代码有问题吗?

我做了一些简单的搜索并尝试了一些修复,但我一定错过了一些基本的东西。

此外,我似乎无法将列表中的所有项目加起来。环顾四周,我还是搞不明白。正如主题所说,我是这方面的新手,所以任何帮助都将不胜感激。

# define variables
new = []
items = float()
tax = float()
final = float()
subtotal = float()
# welcome and set rules of entering numbers
print("Welcome! Sales tax is 7%. Enter your five items, one (1) at a time.")
# gathering input for items
for _ in range(5):
_ += 1
# define condition to continue gathering input
test = True
while test:  # test1 will verify integer or float entered for item1
items = input("Enter the price of item without the $: ")
try:
val = float(items)
print("Input amount is :", "$"+"{0:.2f}".format(val))
test1 = False
except ValueError:
try:
val = float(items)
print("Input amount is :", "$"+"{0:.2f}".format(val))
test = False
except ValueError:
print("That is not a number")
new.append(items)

# define calculations
subtotal = sum(new)
tax = subtotal * .07
final = subtotal + tax
# tax & subtotal
print("Subtotal: ", "$"+"{0:.2f}".format(subtotal))
print("Tax: ", "$"+"{0:.2f}".format(tax))
print("Tare: ", "$"+"{0:.2f}".format(final))

最后不能得到最终计算的原因是循环实际上并没有终止。此外,您的列表由new.append(items)填充字符串。您可能想要new.append(val),这样它就会添加值。

这里有一个版本,它通过@Green Cloak Guy的一个while循环的建议来解决这个问题。我这样做是为了添加任意数量的值;结束";条件,在该条件下,用户键入"end"以退出并获取总数。items.upper()使任何字母大写,因此您可以将其与";结束";,并且仍然捕获";结束"结束";或";eNd";。

#!/usr/bin/python3
# define variables
# you do not have to declare variables in python, but we do have to 
# state that new is an empty list for new.append() to work later
new = []
# welcome and set rules of entering numbers
print("Welcome! Sales tax is 7%. Enter your five items, one (1) at a time.")
print('Type "end" to stop and total items')
# gathering input for items
while True:
items = input("Enter the price of item without the $: ")
if items.upper() == 'END':
break
try:
val = float(items)
except ValueError:
print("Not a valid number.  Try again, or 'end' to stop")
new.append(val)
# define calculations
subtotal = sum(new)
tax = subtotal * .07
final = subtotal + tax
# tax & subtotal
print("Subtotal: ", "$"+"{0:.2f}".format(subtotal))
print("Tax: ", "$"+"{0:.2f}".format(tax))
print("Tare: ", "$"+"{0:.2f}".format(final))

所以问题出在while循环上。

变相地,你的while循环是while True:,并且永远保持这样,直到用户输入了一些分支到except的文本或字符串,但又是这样吗?我非常确信一个except会起作用,因为您的代码正在做两次except,即使它分支

try:
val = float(items)
print("Input amount is :", "$"+"{0:.2f}".format(val))
test1 = False
except ValueError:
try:
val = float(items)
print("Input amount is :", "$"+"{0:.2f}".format(val))
test = False

您可能不需要将第二个try语句作为一个整体,并将其替换为print("That is not a number")test = False

您的控件(_(没有在代码中的任何地方使用,所以我只删除_ += 1

我的理解现在,我认为你想做的是从用户那里获取5个项目,如果用户的输入不是一个合适的浮点值,它会再次询问它,直到用户有5个正确的输入。

您的for循环可以替换为:

首先确保您有一个计数器变量,如vc,并将其分配给0

我们想从用户while获得输入,计数器变量小于5,这是0,1,2,3,4的范围(5倍(。

如果用户输入正确,你可以将计数器增加1,但如果不是,你什么都不做,我们continue

try语句中,第一行可以是你想要测试的任何内容,在这种情况下是float(input(".............")),在这些行下面,如果输入正确且没有抛出错误,你可以添加你想要做的事情,在我们的情况下,将计数器增加一(v += 1(,并将其附加到new。现在,如果用户输入不正确并抛出ValueError,那么except就是我们想要做的,在我们的情况下,它将是continue。当抛出错误时,它直接跳到except,不执行下一行。

这里是获取用户输入的最后一个循环:

v = 0
while v < 5:
items = input("...........")
try:
float(items)
v += 1
new.append(items)
except ValueError:
continue

其余的代码可以是相同的!

#defining variables
v = 0
new = []
items = float()
tax = float()
final = float()
subtotal = float()
#gathering input from items
while v < 5:
items = input("Enter the price of item without the $: ")
try:
float(items)
v += 1
print("Input amount is :", "$"+"{0:.2f}".format(float(items)))
new.append(float(items))
except ValueError:
continue

# define calculations
subtotal = sum(new)
tax = subtotal * .07
final = subtotal + tax
# tax & subtotal
print("Subtotal: ", "$"+"{0:.2f}".format(subtotal))
print("Tax: ", "$"+"{0:.2f}".format(tax))
print("Tare: ", "$"+"{0:.2f}".format(final))

仅此而已!希望你明白为什么。。。

我清理了输出并使用反馈使其更好地工作。现在,如果人们有关于简化代码的建议,那也太好了。我想确保我以一种蟒蛇般的方式写东西,并且从内存使用和代码空间的角度来看也是高效的。

感谢大家到目前为止的帮助!

# define variables
new = []
items = float()
subtotal = float()
tax = float()
final = float()

# welcome and set rules of entering numbers
print("Welcome! Sales tax is 7%. Enter your five items, one (1) at a time.")
# gathering input for items
for _ in range(5):
# define condition to continue gathering input
test = True
while test:  # test1 will verify integer or float entered for item1
items = input("nEnter the price of item without the $: ")
try:
val = float(items)
print("Input amount is :", "$"+"{0:.2f}".format(val))
test = False
except ValueError:
try:
val = float(items)
print("Input amount is :", "$"+"{0:.2f}".format(val))
test = False
except ValueError:
print("That is not a number")
# append items to the list defined as new
new.append(items)
# define calculations
subtotal += float(items)
tax = subtotal * .07
print("Cost Subtotal: ", "$" + "{0:.2f}".format(subtotal), " & Tax Subtotal: ", "$" + "{0:.2f}".format(tax))
_ += 1

# define calculations
final = subtotal + tax
# items tax & subtotal
print("n Final list of item cost:")
new_list = [float(item) for item in new] # fixed this too
for i in new_list:
print("- $"+"{0:.2f}".format(i))
print("n Final Pretax Total: ", "$"+"{0:.2f}".format(subtotal))
print(" Final Tax: ", "$"+"{0:.2f}".format(tax))
print("n Tare: ", "$"+"{0:.2f}".format(final)) 

试试这个方法:

new = []
items = float()
tax = float()
final = float()
subtotal = float()
# welcome and set rules of entering numbers
print("Welcome! Sales tax is 7%. Enter your five items, one (1) at a time.")
try:
new = list(map(lambda x: float(x), list(map(input, range(5)))))
except:
print("Some values are not a number")
# define calculations
subtotal = sum(new)
tax = subtotal * .07
final = subtotal + tax
# tax & subtotal
print("Subtotal: ", "$"+"{0:.2f}".format(subtotal))
print("Tax: ", "$"+"{0:.2f}".format(tax))
print("Tare: ", "$"+"{0:.2f}".format(final))

相关内容

最新更新