我在一个初级Python编程课程中,我们要编写一个程序,生成项目描述、价格和总额。我最初编写的程序使用了列表,这让我在作业中得到了一个胖0,因为显然我们在这项作业中不使用列表。幸运的是,我可以重写它。所以,我应该使用基于计数的迭代结构,我可以使用“for”语句,或者同时使用“for”和“while”语句。但不仅仅是“while”语句(基于计数而不是基于事件)。我不知道哪种组合最有效。我已经附上了我的程序,但我认为它一点也不好,它的结构对我来说似乎很糟糕。如果有人能给我一些指导,告诉我如何让它看起来更好,或者如何改进它,我将不胜感激。我上下搜索了一个例子,其中没有列表或一些疯狂的东西(我是初学者,所以我们使用初学者的东西),但一点运气都没有。 最后,我想再说一次,我不能使用列表,提前谢谢大家
问题:
使用基于计数的迭代结构,该结构将接受下面列出的数据并生成总购买金额。您的最终报告应与下面的报告相似
输入数据:
Item Description Item Price
Salomon Fish $ 26.97
Ribeye Steak $ 12.98
Sweet Corn $ 4.96
Asparagus $ 5.92
输出:
Item Description Item Price
=================================
Salomon Fish $ 26.97
Ribeye Steak $ 12.98
Sweet Corn $ 4.96
Asparagus $ 5.92
Your total purchase: $ xx.xx
我的代码:(如果格式错误,很抱歉,我从未在这里发布过)
fish=int(input("enter fish price ===> "))
corn=int(input("enter corn price ===> "))
steak=int(input("enter steak price ===> "))
asparagus=int(input("enter asparagus price ===> "))
for x in range (1,fish+1,1):
total=x
for y in range(1,corn+1,1):
total=x+y
for z in range(1,steak+1,1):
total=x+y+z
for i in range(1,asparagus+1,1):
total=x+y+z+i
print("Item Description Item Price")
print("==============================")
print("Fish $",fish)
print("Corn $",corn)
print("Steak $",steak)
print("Asparagus $",asparagus)
print("Your Total Purchase:", total)
输出为:
enter fish price ===> 5
enter corn price ===> 5
enter steak price ===> 5
enter asparagus price ===> 5
Item Description Item Price
==============================
Fish $ 5
Corn $ 5
Steak $ 5
Asparagus $ 5
Your Total Purchase: 20
我同意
Mark Meyer
:赋值没有指定输入。您的编程风格已经做得足够好了。鉴于这些限制,我只建议进行几项改进:在这里,跑步记录看起来像:
相关问题 更多 >
编程相关推荐