0

Enter any mathematical expression on the textbox and click the button, the result of the expression will be shown in an alert window. For example, enter 2+3-1 and click the button. It should show 4 in the alert window. I need to use ReactJS to create the webpage.

import React from 'react';
class Addition extends React.Component{
    constructor(){
        super();
        this.state={
        num1:'',
        num2:'',
        total:''
        }
    }

    handlenum1 = (event) => {
        this.setState({
            num1:event.target.value
        })

    }

    handlenum2 = (event) =>{
        this.setState({
            num2:event.target.value
        })

    }
    exe = (event) => {
        this.setState({total:parseInt(this.state.num1) + 
parseInt(this.state.num2)});
        event.prevent.default();

    }

    render(){
        return(
            <div>
            <h1> Addition </h1>
            <form onSubmit={this.exe}>
            <div>
            Number 01:
            <input type="text" value={this.state.num1} onChange={this.handlenum1}/>
            </div>
            <div>
            Number 02:
            <input type="text" value={this.state.num2} onChange={this.handlenum2}/>
            </div>
            <div>
            <button type= "submit"> Add </button>
            </div>
            </form>
            {this.state.total}
            </div>

        )
    }

}
export default Addition;
  • when i check the file,after running the program, the file remains unchanged. what should i do. – Mohsin Shanavas Jul 22 '20 at 17:28
  • Welcome on Stack Overflow. Please clarify your question. There is no 'above code', only your own code (which is fine to post). What is it supposed to do, and what is going wrong? – Ronald Jul 22 '20 at 17:51
  • Code is supposed to change first item in each string in a file. Code is working. But file data is same and not getting updated – Mohsin Shanavas Jul 22 '20 at 18:15
  • I don't see any `write` statement? Where is the code that writes to file? – Ronald Jul 22 '20 at 18:19

1 Answers1

0

First of all, if you open a file in the way you did, after finishing writing\reading from it you should close it- and if you don't the file won't be updated. Second, it's better to avoid this kind of syntax and use the context manager like this:

changed_lines=[]
with open('maksssksksss96.txt','r+')as f:
    for line in f.readlines():
        x = line.split()
        if x[0]  == '15':
            x[0] = '0'
        elif x[0] == '17':
            x[0] = '2'
        elif x[0] == '16':
            x[0] = '1'
        print(listToString(x))
        changed_lines.append(x)
        
with open('newfile.txt', 'w') as newF:
    for line in changed_lines:
        newF.write(line)


def listToString(s):  
    
    # initialize an empty string 
    str1 = " " 
    
    # return string   
    return (str1.join(s))

Replace the dots with the remaining of your code according to what you are trying to achieve

Eliran Turgeman
  • 1,526
  • 2
  • 16
  • 34
  • Apart from 'maksssksksss342.txt' , there are 850 other txt files in same directoy to which i should make same changes. how shoudl i edit code. also, when i use f.close() file is still same. File "C:\Users\Admin\Documents\face mask\text files\text.py", line 16, in for line in f.readlines(): io.UnsupportedOperation: not readable – Mohsin Shanavas Jul 22 '20 at 17:37
  • To get a list of all txt files in your directory look at this question https://stackoverflow.com/questions/3964681/find-all-files-in-a-directory-with-extension-txt-in-python do you call the functions? from the snippet you have copied here you don't call the function that supposed to change the file – Eliran Turgeman Jul 22 '20 at 17:40
  • problems solved. still not able to save file. is it f.close() function – Mohsin Shanavas Jul 22 '20 at 17:49
  • if you use the with statement, you don't need to use ```f.close()``` edit the question with the updated code so I can help – Eliran Turgeman Jul 22 '20 at 18:06
  • newF.write(line) TypeError: write() argument must be str, not list – Mohsin Shanavas Jul 22 '20 at 18:29