+ 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. :/
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!! šš
+ 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!
+ 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
+ 4
David Ashton I think he wants to tell all operations like additional, subtraction, multiplication etc. But your code will just add till "end"
+ 3
+ 2
Yes, Python has an eval function to handle and calculate that kind of string inputs.
+ 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.
+ 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)
+ 2
Rishav Tiwari that could be what Dea meant, but the examples in the question were just addition.
+ 1
Rishav Tiwari yooooo
I needed to know this function before
+ 1
Yes there is a way see this example
num = int(input())
print(eval(num))
0
Alguien sabe como resolver el ejercicio de python fizzBuzz?