Writing a program that solves physics problems in Python

0 Votes
    260 Views

I am having a lot of trouble using for loops with lists, since most of the tutorials online only use the print command. I’ve been having trouble with using the string command together with their associated variables.

I already have the first part running, which finds the value of the number and its place in the “problem” string.

import re
import numpy as np
f = open('sample.txt', 'r')
problem = f.read()
nums = re.findall(r"[-+]?d*.d+|d+", problem)
nums.remove('0')
varLoc = [problem.find(s) for s in nums]
f.close()

I think that it should look something like

string = problem[varLoc + len(nums): varLoc + len(exampleNum) + 5]

This would work if the numbers involved were not lists, but I want it to produce a list of strings that can be checked against a list of possible variables, which also requires using a command on an index, which I’ve been struggling with. I’ll write it here in pseudocode.

exampleVarList = ['V', 'H', 'A']
if [list] contains str from exampleVarList:
 set str variable to nums

With the final output looking something like

V = 300

H = 15

A = 30

To be used in an equation.


Please signup or login to answer this question.