In python if the user doesn't enters a input and simply press submit button. If this is situation then how to set default value? | SoloLearn: Learn to code for FREE!

+7

In python if the user doesn't enters a input and simply press submit button. If this is situation then how to set default value?

How can we add value to input if user forget to do it? https://code.sololearn.com/c1SFki1nKU1O/?ref=app

5/20/2020 1:02:04 PM

NARESH

27 Answers

New Answer

+9

B.Naresh Kumar before you hit the submit button you have to follow some steps only in sololearn Since, your code takes 3 input from the user. So while submitting the values you enter the values in new line Eg - 42 23 32 So, if you want that your code gives the output even if the user doesn't input any value(default value), then still you have to enter space according to your input Eg - (Enter) (Enter) (Enter) I think your doubt may be cleared now😊😊😊 https://code.sololearn.com/cXNshChP7mzu/?ref=app

+16

choice = input("") or 7 choice takes input if not empty, otherwise 7.

+8

Divyanshu Singh u satisfied me😊

+6

Most welcome brother. Feel free to ask anything related to Python language

+6

Divyanshu Singh sure bro😊😊

+5

#B.Naresh Kumar look at this once: #if only single input needed, use num1=int(input() or 70) #for more inputs use try,catch like try: num2=int(input()) num3=int(input()) except : num2 =30 num3 =20

+5

👑 Prometheus 🇸🇬 Subash Rogers Jubin Jacob BroFar Brahmeswara Rao Kung Fu Panda pls help me

+4

n = [1,2,3] choice = input('Pick a number from 1-3') if choice == '' or choice not in n: choice = 1

+4

Slick and Jayakrishna🇮🇳 please give your answer according my code .I edited the qn.😊

+4

You must be a beast with another language haha. Python uses "None" in place of "null"

+4

Slick it doesn't worked☹️

+4

Slick replaced but literal error☹️

+4

Theres a bunch of ways to do this but im not trying to rewrite your code my man. You just called in a squad so Ill write my version of the program and you can see if theres anything to implement.

+4

I did not find any mistake. It's working well. May be , I don't understand what you are asking!😀

+4

One thing I like about Python is precisely the fact that you can do this in a rather elegant way. For example if input is stored in variable n you can do: n=int(input() or 5) If the user just press Submit, then n will be set to 5

+3

B.Naresh Kumar what problem you are getting still.. If only single input need then use or method.. Because a press enter treated as empty string... If multiple inputs, then go for try catch method,...

+3

Try this dude: a = input() if len(a) is 0: a = 'value' I think this the possible way in Sololearn

+3

Parsa Khadivpoor thank u

+2

Python is also case sensitive sir. Exactly whats in the quotes above is what needs to be replaced

+1

https://code.sololearn.com/cHFiXxl9dCSc/?ref=app