473,466 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Performing mathematical operations on list entries

4 New Member
I'm learning Python, and as a small exercise I made for myself I'm writing a program to find the determinate of a 2x2 matrix. I'm running Python 2.5 on Windows Vista, here's my code.

Expand|Select|Wrap|Line Numbers
  1. #for loop test.
  2. #Finding the determinate of a 2x2 matrix
  3.  
  4. matrix = []
  5.  
  6. newelement = raw_input("Enter your first value: ")
  7. matrix.append(newelement)
  8.  
  9. while len(matrix) < 4:
  10.     nextelement = raw_input("enter your next element: ")
  11.     matrix.append(nextelement)
  12.  
  13. int(matrix[0])
  14. int(matrix[1])
  15. int(matrix[2])
  16. int(matrix[3])
  17. determinate = (matrix[0]*matrix[3])-(matrix[1]*matrix[2])
  18. print "The determinate of your matrix is "+ determinate +"."
  19.  
The error I get tells me that the elements of matrix are strings and thus the * operation cannot be performed on them.

I'm a bit embarrassed to ask such a simple question, but I really know very little about Python and I've done as much research as I can given the resources I have (how I found int() which doesn't seem to work). Advice?
Apr 11 '08 #1
1 1848
bvdet
2,851 Recognized Expert Moderator Specialist
I'm learning Python, and as a small exercise I made for myself I'm writing a program to find the determinate of a 2x2 matrix. I'm running Python 2.5 on Windows Vista, here's my code.

Expand|Select|Wrap|Line Numbers
  1. #for loop test.
  2. #Finding the determinate of a 2x2 matrix
  3.  
  4. matrix = []
  5.  
  6. newelement = raw_input("Enter your first value: ")
  7. matrix.append(newelement)
  8.  
  9. while len(matrix) < 4:
  10.     nextelement = raw_input("enter your next element: ")
  11.     matrix.append(nextelement)
  12.  
  13. determinate = (int(matrix[0])*int(matrix[3]))-(int(matrix[1])*int(matrix[2]))
  14. print "The determinate of your matrix is "+ determinate +"."
  15.  
The error I get tells me that the elements of matrix are strings and thus the * operation cannot be performed on them.

I'm a bit embarrassed to ask such a simple question, but I really know very little about Python and I've done as much research as I can given the resources I have (how I found int() which doesn't seem to work). Advice?
You were very close. You can modify the contents of a list, but you have to assign the modified value to the list element. Example:
Expand|Select|Wrap|Line Numbers
  1. matrix[0]=int(matrix[0])
I modified your code in the quoted post above, which is another option. Yet another way:
Expand|Select|Wrap|Line Numbers
  1. newelement = int(raw_input("Enter your first value: "))
HTH :)
Apr 11 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Paul van Hagen | last post by:
Hello, I've been doing some research as to ways to optimise our parsing and evaluation of mathematical expressions and came across the spirit template library as part of the boost library. It...
3
by: Alexander Pucher | last post by:
Hi, given a table with some data, e.g. some monthly measures. Some of the measures are missing though. id m1 m2 m3 m4 m5 .... m12...
3
by: Lisa Burks | last post by:
I have two problems I am trying to solve. 1) I am needing to be able to double-click on a list and have it open another form with the details. I will need the detailed form to be a stand alone...
2
by: rameshadari | last post by:
Hello Guys, I am new to SQL Server Database programming. My specific question is:- Is it possible to do some mathematical operations on columns present in a table. If possible, How? For...
9
by: tomamil | last post by:
imagine that you have different matrices with different names and you want to perform the same action with each of them. is it possible to put their names into some array and to create a loop that...
6
by: Henrik Goldman | last post by:
Hello, I have a dataset which consist of a string username and string hostname as a key and then an integer representing a count as the matching "second" value in a pair. So far I've used...
2
by: Lysander | last post by:
I have not seen this feature documented before, so I thought I would share it with you, as I will be using it in a later article. For a combo or list box, the source data is normally a...
2
by: Madmartigan | last post by:
Hi Operating system is WinXP using SharpDevelop version 1.1.0 and build 2124. I'm new to C# and have a problem trying to get a user to enter 3 mathematical operators of choice, then 2 numbers...
5
by: jacob navia | last post by:
The cephes math library is a legend in the internet. It was written by Stephen L. Moshier in 1984, but there was apparently an even earlier version in assembler, before he rewrote that in C. It is...
5
by: JvC | last post by:
Background Access 2003. MDB Front End. I am populating a list box with the names of queries in the system. I am searching for strings in the query names and then creating a value list for users...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.