Python - Function print vs return - whats wrong


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Python - Function print vs return - whats wrong
# 1  
Old 04-23-2015
Python - Function print vs return - whats wrong

All,

I have a basic buzz program written in python with return function. If i change return with print,it works fine but i want to know whats wrong with return statement.Can anyone help me whats wrong with this
Code:
#!/usr/bin/python

def div4and6(s,e):
    for i in range(s,e+1):
         if i%4 == 0 and i%6 == 0:
           return "Hello"
         elif i%4 == 0:
           return "Hell"
         elif i%6 == 0:
            return "Hel"
         else:
            return i

a=input("Please input the start and end no: ")
b=input("Please input the start and end no: ")
print div4and6(a,b)


Last edited by oky; 04-23-2015 at 03:23 PM..
# 2  
Old 04-23-2015
1. You have indentation errors in your code. Indentation is very important for python.

2. If python sees a return statement, it will simply goes out of the function back to the caller; regardless of the loop it is in. So your logic would be wrong if it's a return instead of print.

3. If this is a classwork/homework question, there's a different section in this forum.
# 3  
Old 04-23-2015
balajesuri,

Sorry when i typed the code intentation changed.

However this is the output

Code:
Please input the start and end no: 1
Please input the start and end no: 20
1

Whereas i am expecting the following output

Code:
1
2
3
Hell
5
Hel
7
Hell
9
10
11
Hello
13
14
15
Hell
17
Hel
19
Hell
None

not sure where it is going wrong
# 4  
Old 04-23-2015
Is this a homework item? Until you answer this question, we are unable to provide much more help.

You know indentation is important in python scripts. But the script you posted has indentation that is different from the code you're using. How do you expect us to help debug your code if you don't show us an accurate representation (in CODE tags) of the code you're using?
# 5  
Old 04-23-2015
Don,

This is not a homework problem. I have corrected the code now its looking good
# 6  
Old 04-23-2015
As balajesuri said, the first time through the loop in your function (with i set to 1) ends with the statement:
Code:
return i

and the return statement terminates the function and returns to the caller. If you change all of the return x statements in your function to print x statements and change the call to your function from:
Code:
print div4and6(a,b)

to just:
Code:
div4and6(a,b)

it looks like you would get what you want.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Whats wrong with this if-else

hi whats wrong in below?? CHECK=M10; if ; then echo "hello hi"; else echo "how are u hello hi"; fi I am getting error as ./test.sh: line 2: ' ./test.sh: line 2: M10: command not found ./test.sh: line 2: M10: command not found ./test.sh: line 2: M10: command not found (8 Replies)
Discussion started by: skyineyes
8 Replies

2. Homework & Coursework Questions

Whats wrong with the following

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: ls -ld htdocs drwxr-x--- 3 root root 8192 2006-11-19 10:41 htdocs How would a host administrator... (1 Reply)
Discussion started by: Larry_1
1 Replies

3. Shell Programming and Scripting

Whats wrong in the Function ?

Need your assistance, to find the bug in the function. Function usage erroring out even after passing parameters. usage() { if || ; then echo "************************************************************" echo " CHECK USAGE FOR CORRECT PARAMETERS ... (26 Replies)
Discussion started by: raghunsi
26 Replies

4. Shell Programming and Scripting

whats wrong in my code

Code: #!/usr/bin/perl -w use strict; use warnings; #Clears Screen $CLEAR=`clear`; print $CLEAR; i get the below error: Global symbol "$CLEAR" requires explicit package name at ./mutmg.pl line 6. Global symbol "$CLEAR" requires explicit package name at ./mutmg.pl line 7. (1 Reply)
Discussion started by: sophos
1 Replies

5. UNIX for Dummies Questions & Answers

whats wrong with this?

can anyone tell me why this code doesn't work how its supposed to, its the hangman game but it doesn't play how its supposed to #!/bin/bash NoAttempts="0" livesgiven="5" LivesRemain=$livesgiven LettersAttempted="" wordfile=words numwords=0 function menu() { clear cat << menu... (1 Reply)
Discussion started by: ferrycorsten73
1 Replies

6. UNIX for Dummies Questions & Answers

Whats wrong in the script?

if then if then echo "fst argument is $1 " else if then "fst argument is $1" fi fi fi Can anyone tell me. My requirement is tht pass a string .. Check whether it contains "-". If yes then check if it... (1 Reply)
Discussion started by: nehagupta2008
1 Replies

7. Shell Programming and Scripting

whats wrong with this code

ls -ld | grep $1 /etc/passwd | cut -d: -f6 i need see the content... (4 Replies)
Discussion started by: nadman123
4 Replies

8. Shell Programming and Scripting

tell me whats wrong with this

#! /bin/bash USAGE=" | ] if then echo "$USAGE" exit 1 fi while getopts lb: OPTION do case $(OPTION)in a) echo Hi there! exit 2;; b) echo hello o) OARG=$OPTARG;; \?)echo "$USAGE" ;; exit 2;; esac done shift `expr... (1 Reply)
Discussion started by: nadman123
1 Replies

9. Shell Programming and Scripting

tell me whats wrong in this?

#! /bin/bash head -5 $1 echo "remove $1 ?" read answer if then echo invalid answer elif rm $1 echo "$1 is deleted" elif then echo file is not deleted else echo "invalid answer" fi What i really want this to do is to ask to delete the file or not..it says something wrong... (1 Reply)
Discussion started by: nadman123
1 Replies

10. Shell Programming and Scripting

Whats wrong with my function?? <newbie>

First of all im using Bash, on a Debian-based machine. I tried to write a function that if the ls program found listed more than 25 lines I would automaticly use "ls | less". Its on another computer but if I recall it looked something like this... Note: some code may look strange because im on... (4 Replies)
Discussion started by: riwa
4 Replies
Login or Register to Ask a Question