Python Forum

Full Version: Meal cost challenge
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want to solve a meal cost challenge as part of learning python but when I put the following code which the website gave me, I had two problems:
import math
import os
import random
import re
import sys

# Complete the solve function below.
def solve(meal_cost, tip_percent, tax_percent):

if __name__ == '__main__':
    meal_cost = float(input())

    tip_percent = int(input())

    tax_percent = int(input())

    solve(meal_cost, tip_percent, tax_percent)
The first problem is: 1. My editor gave an Indentation error on the if block. It says it expected an indented block. What does that mean and how do I resolve it?
Second problem: What does the if block mean. i.e
if __name__ == '__main__':...
. What does it actually mean? In my readings I have not come across such an expression. Just a noob still therefore why I am taking the challenge to improve.
Thanks for your help.
The editor gives an indentation error because the solve() function started above needs a minimal indented body. You can use a simple pass statement
def solve(...):
    pass
When python runs code from a file, there is a variable called __name__ in the current namespace. If the value of this variable is the string "__main__", it means that the current namespace is the main code of the program (as opposed to an imported library module). The test if __name__ == "__main__": is semantically equivalent to the human sentence if we are in the main program and not in an imported module. Its purpose is to allow one to write files that can be run as main programs or imported as modules. Under this test, one writes parts of the code that will be ran only when this file is used as the main program.
You need to define a function solve, i.e. create the body of the function. That is your assignment.
@Gribouillis suggest pass, but that will just deal with the current error. Instead of pass you need to write your code for teh function
lines #10-17 are there to help you test the function you will write
(Jun-01-2020, 11:23 AM)buran Wrote: [ -> ]You need to define a function solve, i.e. create the body of the function. That is your assignment.
@Gribouillis suggest pass, but that will just deal with the current error. Instead of pass you need to write your code for teh function
lines #10-17 are there to help you test the function you will write

Okay, it's a baby assignment though. I just didn't understand the reason for the error. Thanks