+ 1
How to fix indentation problem in Python?
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")
+ 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...
+ 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 ;)
+ 7
@visph Didn't know that, thanks!
+ 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.
+ 3
@Casper N'drih: please, attribute best answer mark to a real good answer ^^
+ 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 ^^
+ 2
@visph thankyou ☺☺🙋
+ 2
@Kuba: really? I was thinking you just had shortcut/simplify your explanation ;)
+ 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.. 🙋☺☺🙏👏
+ 1
Wow! You really explained it well thankyou.. ☺☺☺
+ 1
Thankyou brother... ☺☺☺
0
okay..
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")
0
@visph Wow! thanks again mate... that's the fix I'm looking for, now it seems to be clear!