+ 1

How to fix indentation problem in Python?

4th Feb 2018, 6:39 AM
Casper N'drih
Casper N'drih - avatar
15 Respuestas
+ 5
while True: print("Options:") print("Enter 'add' to add two numbers") print("Enter 'subtract' to subtract two numbers") print("Enter 'multiply' to multiply two numbers") print("Enter 'divide' to divide two numbers") print("Enter 'quit' to end the program") user_input = input(": ") if user_input == "quit": break elif user_input == "add": num1 = float(input("Enter a number: ")) num2 = float(input("Enter another number: ")) result = str(num1 + num2) print("The answer is " + result) elif user_input == "subtract": num1 = float(input("Enter a number: ")) num2 = float(input("Enter another number: ")) result = str(num1 - num2) print("The answer is " - result) elif user_input == "multiply": num1 = float(input("Enter a number: ")) num2 = float(input("Enter another number: ")) result = str(num1 * num2) print("The answer is " * result) elif user_input == "divide": num1 = float(input("Enter a number: ")) num2 = float(input("Enter another number: ")) result = str(num1 / num2) print("The answer is " / result) else: print("Unknown input")
4th Feb 2018, 8:06 AM
visph
visph - avatar
+ 9
Simply didn't seem too Pythonic to mix the indent styles ;) On the other hand, that's good when you think about collaborative work on a code...
4th Feb 2018, 7:34 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 7
# @Kuba advice is a (very) good practice, but that's not exactly true: indentation must be consistent and preserve same number of spaces OR tabulations (but not mix of both) only in a block, not through the whole file: ans = int(input('Enter a number: ')) if ans == 42: print('Congratulation!') print('You\'ve got THE answer') else: print('You failed!') print('Try again') # ... is perfectly valid, but will quickly lack of readability, and so, favorize indentation mistakes ;)
4th Feb 2018, 7:24 AM
visph
visph - avatar
+ 7
@visph Didn't know that, thanks!
4th Feb 2018, 7:29 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 6
You have to stay consistent and preserve the same indentation step throughout your code. If the first level indent is of depth 4, the second level has to be 8, 3rd - 12 and so on.
4th Feb 2018, 6:50 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 3
@Casper N'drih: please, attribute best answer mark to a real good answer ^^
4th Feb 2018, 7:39 AM
visph
visph - avatar
+ 3
@Casper: My last comment is no more a real good answer than the previous (thanks of Kuba for my answer)... A "real" good answer, is a (good) answer related to the thread main question, obviously the best one ^^
4th Feb 2018, 8:00 AM
visph
visph - avatar
+ 2
@visph thankyou ☺☺🙋
4th Feb 2018, 7:29 AM
Casper N'drih
Casper N'drih - avatar
+ 2
@Kuba: really? I was thinking you just had shortcut/simplify your explanation ;)
4th Feb 2018, 7:32 AM
visph
visph - avatar
+ 2
@visph okay....as for me I'm just new to python that's why problem like this I don't have solution to solve such thing that's why I need experts 😊 like you and @Kuba to help...by the way thanks much to you two.. 🙋☺☺🙏👏
4th Feb 2018, 7:54 AM
Casper N'drih
Casper N'drih - avatar
+ 1
Wow! You really explained it well thankyou.. ☺☺☺
4th Feb 2018, 6:52 AM
Casper N'drih
Casper N'drih - avatar
+ 1
Thankyou brother... ☺☺☺
4th Feb 2018, 7:19 AM
Casper N'drih
Casper N'drih - avatar
0
okay..
4th Feb 2018, 7:16 AM
Casper N'drih
Casper N'drih - avatar
0
@visph please have a look at this code and help fix! while True: print("Options:") print("Enter 'add' to add two numbers") print("Enter 'subtract' to subtract two numbers") print("Enter 'multiply' to multiply two numbers") print("Enter 'divide' to divide two numbers") print("Enter 'quit' to end the program") user_input = input(": ") if user_input == "quit": break elif user_input == "add": num1 = float(input("Enter a number: ")) num2 = float(input("Enter another number: ")) result = str(num1 + num2) print("The answer is " + result) elif user_input == "subtract": num1 = float(input("Enter a number: ")) num2 = float(input("Enter another number: ")) result = str(num1 - num2) print("The answer is " - result) elif user_input == "multiply": num1 = float(input("Enter a number: ")) num2 = float(input("Enter another number: ")) result = str(num1 * num2) print("The answer is " * result) elif user_input == "divide": num1 = float(input("Enter a number: ")) num2 = float(input("Enter another number: ")) result = str(num1 / num2) print("The answer is " / result) else: print("Unknown input")
4th Feb 2018, 8:03 AM
Casper N'drih
Casper N'drih - avatar
0
@visph Wow! thanks again mate... that's the fix I'm looking for, now it seems to be clear!
4th Feb 2018, 8:09 AM
Casper N'drih
Casper N'drih - avatar