+ 3

How to make an infinite calculator in python?

How do I fix my code so that the Calculator takes the numbers from the user and adds them up until the user writes "stop". https://code.sololearn.com/cDMR9Y626m5T/?ref=app

7th Mar 2022, 12:09 PM
denter tv
denter tv - avatar
10 Answers
+ 2
Yes. As I already told, it only works for numbers asking infinitely.. And other than input "stop" Or numbers, it will raise error.. Check input : 2 3 stop It's fine for above But 2 3 SSS 2 It raise error.. In your case, it asking input again after 10. If no input then it raise error. See this code : It uses try catch , works fine for infinite number of input until you enter "stop".. . if you not aware of try-catch, then you need to learn first. while True : i =input() print ("god job(for test)") try : print(int(i) + int(i)) except : if i == "stop": break
7th Mar 2022, 2:06 PM
Jayakrishna 🇮🇳
+ 4
Sololearn doesn't really support that since i guess they only use one time input. Try using pydroid3
8th Mar 2022, 12:30 PM
eskayML 💣
eskayML 💣 - avatar
+ 3
Ok, thanks, but how now to make an endless loop for receiving data from the user? It just doesn't work. https://code.sololearn.com/cMbv4Ee84RJn/?ref=app
7th Mar 2022, 1:17 PM
denter tv
denter tv - avatar
+ 2
I suggest you to make it correct for 1 time. Then add that code in a infinite loop. your code has identation errors. Correct it.. input() has a type mistake. and you are taking number to i but comparing with string.. it's error again. Your string input is into b variable..
7th Mar 2022, 12:29 PM
Jayakrishna 🇮🇳
+ 2
Thank you. but I have not studied this try method yet, but thanks for the hint. Good luck to you
7th Mar 2022, 2:09 PM
denter tv
denter tv - avatar
+ 2
You're welcome..
7th Mar 2022, 2:12 PM
Jayakrishna 🇮🇳
7th Mar 2022, 12:58 PM
denter tv
denter tv - avatar
+ 1
i =input() while True: print("god job(for test)") if int(i): #here if input is not a number then it raise error, and execution stoped here . if it is a number then its an infinite loop print (int(i) + int(i)) else i = "stop": # its invalid statement. Are you comparing i with "stop" ? then it must be like elif i=="stop" : # * use elif *. it wont work here for taking string as input as per your code #if you assigning then use else : i="stop" break #is it passing your first step, taking of numbers? think about it.
7th Mar 2022, 1:06 PM
Jayakrishna 🇮🇳
+ 1
Its already infinite loop for integer input but converting to int of non-compatibles from string will raise error. Ex: int("2") is fine, but int("stop") will raise error. So you can't work on single input here. You need another input for string. If you reverse logic, then it works like : First compare in if block. then convert in else block . Like : while True : i =input() if i == "stop": break else : print ("god job(for test)") print (int(i) + int(i)) But if you input other than " Numbers " Or string "stop", it will raise error. You can do it by try catch block if you want to go by single input.. Or take other input to stop
7th Mar 2022, 1:43 PM
Jayakrishna 🇮🇳
+ 1
Thank you for your help and solving this problem. As I understand it, everything will work correctly on the pc, just in the application after I entered the data and the first cycle ended (I entered 10), it gives the following error: output. god job (for test) 20. Traceback (most recent call last): File "file0.ru", 1ine 2, in <module> i = input() EOFError: EOF when reading a line https://code.sololearn.com/cbclzve92G5V/?ref=app
7th Mar 2022, 1:56 PM
denter tv
denter tv - avatar