## I need to Optimize this code.

This is the place for queries that don't fit in any of the other categories.

### I need to Optimize this code.

I have been given this code to optimize and I think you have to add an array and a loop, if you are able to help me could you explain what you have done please.

Code: Select all
`item1 = int(raw_input("Please enter the amount for item 1")) item2 = int(raw_input("Please enter the amount for item 2"))item3 = int(raw_input("Please enter the amount for item 3"))item4 = int(raw_input("Please enter the amount for item 4")) item5 = int(raw_input("Please enter the amount for item 5")) total = 0total = total + item1total = total + item2total = total + item3total = total + item4total = total + item5 print (str(total))`
Last edited by stranac on Sun Oct 06, 2013 12:45 pm, edited 1 time in total.
Reason: Locked
Tristian_Ruben

Posts: 1
Joined: Sun Oct 06, 2013 10:04 am

### Re: I need to Optimize this code.

You are expected to show an attempt you've made.
There is plenty of information on loops and lists(I doubt you want arrays) in the python tutorial, as well as in any book you might be learning from.
Friendship is magic!

R.I.P. Tracy M. You will be missed.

stranac

Posts: 1790
Joined: Thu Feb 07, 2013 3:42 pm

### Re: I need to Optimize this code.

What do you mean "optimize"? I would change this code to use a loop, because it's better convention, but when we usually talk about efficiency we mean time complexity (big-O) which can't really change here.
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

micseydel

Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA