+ 2

Is there a way for Python to calculate what the user enters all at once?

Is there a way for Python to calculate whatever a user enters, regardless of how long it is? For example, say the user does x + x + x and then wants to know x + x - how would I make it so, in the same program, Python calculates whatever the user enters? I'm somewhat new so sorry if I don't make sense. :/

23rd May 2021, 6:15 AM
Dea Mer
12 Answers
+ 6
David Ashton Yeah, you are right!! That's why I need some more examples šŸ˜‚ , but he/she have mentioned Calculator so maybe he/she want to tell about all simple calculators operations šŸ¤” , now Dea Mer can only say that what he/she wants!! šŸ˜‚šŸ˜‚
23rd May 2021, 7:04 AM
Abhiyantā
Abhiyantā - avatar
+ 5
Umm, can you give a sample Input and Sample Output for show example!! And you have to show your attempts also so we can know that what is wrong with your code!
23rd May 2021, 6:28 AM
Abhiyantā
Abhiyantā - avatar
+ 4
If you are thinking to make a calculator just like modern calculators do as many calculations you can do Then as Quantum Said use eval function. And instead of x use some numbers. https://code.sololearn.com/cUWx1E4cfWN9/?ref=app
23rd May 2021, 6:43 AM
Abhiyantā
Abhiyantā - avatar
+ 4
David Ashton I think he wants to tell all operations like additional, subtraction, multiplication etc. But your code will just add till "end"
23rd May 2021, 6:52 AM
Abhiyantā
Abhiyantā - avatar
+ 3
Quantum David Ashton Rishav Tiwari Thanks guys!! :) Very much appreciated
23rd May 2021, 10:06 PM
Dea Mer
+ 2
Yes, Python has an eval function to handle and calculate that kind of string inputs.
23rd May 2021, 6:27 AM
Jan
Jan - avatar
+ 2
The computer needs to be told when the last item has been input (otherwise it will wait forever for the next input). You can do this by making the first input the number of inputs that will follow, or you can end your inputs with a signal like "end". Then you can use a "while" loop to accept inputs until the last entry is input and then do the calculation Don't forget that in SoloLearn, all inputs must be done at once.
23rd May 2021, 6:32 AM
David Ashton
David Ashton - avatar
+ 2
Here are examples of the two methods I described. n = int(input()) count = 0 total = 0 while count < n: total += int(input()) count += 1 print(total) total = 0 while True: inp = input() if inp == "end": break total += int(inp) print(total)
23rd May 2021, 6:55 AM
David Ashton
David Ashton - avatar
+ 2
Rishav Tiwari that could be what Dea meant, but the examples in the question were just addition.
23rd May 2021, 6:58 AM
David Ashton
David Ashton - avatar
+ 1
Rishav Tiwari yooooo I needed to know this function before
23rd May 2021, 7:15 AM
Sadra Shakouri
Sadra Shakouri - avatar
+ 1
Yes there is a way see this example num = int(input()) print(eval(num))
24th May 2021, 8:51 AM
Chris Jonathan
Chris Jonathan - avatar
0
Alguien sabe como resolver el ejercicio de python fizzBuzz?
25th May 2021, 2:46 AM
rhanzess guerrero
rhanzess guerrero - avatar