Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Meal cost challenge
#1
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.
Reply
#2
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.
Reply
#3
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Team Chooser Challenge kanchiongspider 3 2,365 Jun-02-2020, 04:02 AM
Last Post: kanchiongspider
  Appending To Files Challenge erfanakbari1 3 2,930 Mar-27-2019, 07:55 AM
Last Post: perfringo
  Problem with a basic if challenge erfanakbari1 2 1,988 Oct-12-2018, 08:04 AM
Last Post: erfanakbari1
  trying an online challenge tozqo 8 5,958 Jun-21-2017, 07:07 AM
Last Post: Kebap

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020