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.

Postby Tristian_Ruben » Sun Oct 06, 2013 10:11 am

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 = 0
total = total + item1
total = total + item2
total = total + item3
total = total + item4
total = 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.

Postby stranac » Sun Oct 06, 2013 12:44 pm

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.
User avatar
stranac
 
Posts: 1246
Joined: Thu Feb 07, 2013 3:42 pm

Re: I need to Optimize this code.

Postby micseydel » Mon Oct 07, 2013 2:42 am

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.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1506
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot] and 6 guests