While+break calculator practice (lesson 26.1) | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 1

While+break calculator practice (lesson 26.1)

Unsure how to take/sort both int+string input and how to input my sum equation to tally only once. Been butting my head on this for a few days and haven't asked for any help so far but I'm sure it's not a super complex solution and I wanna move past it aha Here is my current code: sum = 0 while True: x = input() #your code goes here sum += int(x) print(sum) This seems to be a working calculator, but I'm unsure how to: a) have it sum the inputs only once, as the output asks for a single value (I'm sure its a matter of putting print sum at a different line or indent level, not sure exactly how though) b) take both integer inputs (since input defaults as string) as well as string, to process both number values and the 'stop' command. Ideas have included adding lines such as: x = int(input()) If x #or input() == int()/int(input()): sum += x if x #or input() == ("stop") #or str("stop"): break

11th Feb 2021, 7:05 PM
Dom
2 Respostas
+ 7
Dom , here is your code with some comments and hints. this should help you solving this task: sum = 0 while True: x = input() #your code goes here # use a if... else... construct for the following code lines # check if input is "stop" # if YES, use break to exit the loop # if NO, use the following line sum += int(x) # as the sum has to be printed only once, the print starement in next line has to be outside the while loop print(sum)
11th Feb 2021, 7:42 PM
Lothar
Lothar - avatar
+ 1
This is awesome, thank you! Hints are much better than the answer šŸ™ŒšŸ¾ didn't consider reversing the order so break comes before sum, I did try putting print outside of the loop but encountered errors. I'll give this a go :)
11th Feb 2021, 7:50 PM
Dom