beginner Python program using count-based iteration structure

懵懂的女人 提交于 2020-12-09 06:42:53

问题


I am in a beginner Python programming class and we were to write a program that generates an item description, it's price, and the total. The program I originally wrote used LISTS which landed me a fat 0 for the assignment because apparently we were not to use lists on this assignment. Fortunately for me I get to rewrite it. SO, I am supposed to use a count-based iteration structure, I can use the “for” statement, or both the “for” and “while” statements. But NOT just the “while” statement alone ( count based not event based). I do not know which combination would be most effective. I have attached my program but I do not think it is very good at all, the structure just seems bad to me. If anyone could give me some guidance on how I could make this look better, or how I could improve it I would appreciate it immensely. I have searched high and low for an example that doesn't include lists or some crazy stuff (I am a beginner so we use beginner things) but have had no luck at all. Lastly, I would like to say again, I CANNOT use LISTS, thank you all in advance.

The Problem:

Utilise a count-based iteration structure that will accepts the data listed below and produce the total purchase amount. Your final report should be similar to the one show below.

Input Data:

Item Description    Item Price
Salomon Fish        $ 26.97
Ribeye Steak        $ 12.98
Sweet Corn          $ 4.96
Asparagus           $ 5.92

Output:

Item Description       Item Price

=================================

Salomon Fish            $ 26.97

Ribeye Steak            $ 12.98

Sweet Corn              $ 4.96

Asparagus               $ 5.92

Your total purchase: $ xx.xx

My Code: (sorry if formatting is wrong, I have never posted on here)

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)

the output is:

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

回答1:


I concur with Mark Meyer: the assignment does not specify the input. You've done well enough with your programming style. Given the restrictions, I suggest only a couple of improvements:

  • Use a running total variable for the sum.
  • Give it a meaningful name. x, y, z are meaningless.

Here, the running tally would look something like:

total = 0
total += fish
total += corn
...


来源:https://stackoverflow.com/questions/64830081/beginner-python-program-using-count-based-iteration-structure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!