+ 2

Is there a way to simplify this code?

s = input('How do you like, placeholder, on a scale from 1-10?') print(s) if s=="1": print('custom entry here') if s=="2": print('custom entry here') if s=="3": print('custom entry here') if s=="4": print('custom entry here') if s=="5": print('custom entry here') if s=="6": print('custom entry here') if s=="7": print('custom entry here') if s=="8": print('custom entry here') if s=="9": print('custom entry here') if s=="10": print('custom entry here') else: print('That is not a valid entry')

23rd Apr 2017, 1:17 AM
Kolby
11 odpowiedzi
+ 8
responses=( 'custom 1', 'custom 2', # etc ) s = int(input('How do you like, placeholder, on a scale from 1-10?')) try: print("{}\n{}".format(s, responses[s-1])) except IndexError: print('That is not a valid entry')
23rd Apr 2017, 2:29 AM
Kirk Schafer
Kirk Schafer - avatar
+ 4
dont python but if it were c or vb i would use a switch/case statement.. but i hear tell python doesnt have one.. I found this. it might be of help. http://blog.simonwillison.net/post/57956755106/switch oh and this: http://stackoverflow.com/questions/60208/replacements-for-switch-statement-in-python
23rd Apr 2017, 2:13 AM
jay
jay - avatar
+ 2
Hi Kolby, yes, there is! s = input('How do you like, placeholder, on a scale from 1-10?') print(s) if int(s) in range(1,11): print('custom entry here') if s!="10": print('That is not a valid entry') These lines produce the same output, as your code does. However I am not sure, your code does what you expect it to do. I guess, you want to print “...custom...“ if the input is an integer between 1 and 10. And if it is not, you want to print “...not valid...“, right? If you replace all 'if' by 'elif' in your code, except for the first one, your code will work this way. Also for this, there will be a shorter version, similar to the one I posted :-) Best and have fun! nmc
23rd Apr 2017, 2:08 AM
needmorechocolate
needmorechocolate - avatar
+ 2
If messages are different for each scale, you can use something like this: scale = input('Please enter a scale from 1-10?') print(scale) scale_messages = { '1': 'custom message 1', '2': 'custom message 2', '3': 'custom message 3', '4': 'custom message 4', '5': 'custom message 5', '6': 'custom message 6', '7': 'custom message 7', '8': 'custom message 8', '9': 'custom message 9', '10': 'custom message 10', } message = scale_messages.get( scale, 'That is not a valid scale') print(message)
23rd Apr 2017, 6:02 AM
Igor B
Igor B - avatar
+ 1
Thank you
23rd Apr 2017, 2:49 AM
Kolby
+ 1
So I changed the code to:s = input('How do you like, placeholder, on a scale from 1-10? ') print(s) if int(s)in range(1,11): print('Custom entey here') if s > 10: print('That is not a valid entry') I thought that a > would be more appropriate than != because in the case you wrote in it only works for the specified number. But now I get a Type error: unorderable types, str() > int()
23rd Apr 2017, 3:08 AM
Kolby
+ 1
Mayur Chaudhari almost gave you the implementation of what I suggested. However, there are still three errors in this code snippet.
23rd Apr 2017, 3:32 AM
needmorechocolate
needmorechocolate - avatar
0
s = input("How do you like,placeholder,on a scale from 1-10"); for s in range(1,10): print("custom entry here") else: print("Invalid Entry")
23rd Apr 2017, 3:16 AM
Mayur Chaudhari
Mayur Chaudhari - avatar
0
Hi Kolby, you are on the correct way with your revised code. You got this error because your variable s is still a string (which you can't compare with an integer). You can handle this in two ways: 1. cast the string s into an integer, as in the first if-statement. or 2. replace your second if-statement by using else. Doing so, you will also detect invalid user input values that are less than 1, such as 0. Option two is the 'cleanest' way.
23rd Apr 2017, 3:19 AM
needmorechocolate
needmorechocolate - avatar
0
Thank you the code now works
23rd Apr 2017, 3:33 AM
Kolby
- 1
its easy by using loop here
23rd Apr 2017, 3:34 AM
Mayur Chaudhari
Mayur Chaudhari - avatar