The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > Python


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 11-02-2007, 07:00 AM
ViewSonic20061 ViewSonic20061 is offline
Novice
Join Date: Nov 2007
Posts: 1
iTrader: (0)
ViewSonic20061 is on a distinguished road
Help with Python

Does anyone know how to make the program running until the ouput becomes 1? If hail(x) = x/2 if x is even(i.e., x%2 is 0) and hail(x) = 3x+1 if x is odd(i.e., x%2 is 1)

example:
Please enter a starting value: 3
Output will be:
3
10
5
16
8
4
2
1

so far wat i came up with was:
Code:
x =  int(raw_input("Please enter a starting value:"))

def hail(x):
    if x % 2 == 0:
        return x / 2
    else:
        return (3*x)+1
output = hail(x)
print output
but i still cant seem to think of a way to make them repeat itself and keep caculating till it becomes one. Any ideas or suggestions?

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by TeraTask : 11-03-2007 at 01:34 PM. Reason: Added code tags
Reply With Quote
  #2 (permalink)  
Old 11-02-2007, 07:16 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,111
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Sorry I'm unfamiliar with Python, but if you've the function already...

Quote:
hail(x) = x/2
and

Quote:
hail(x) = 3x+1
Then write something like:

Pseudo code:
Code:
Integer Function hailEven(Integer x)
     return x/2
End Function hailEven

Integer Function hailOdd(Integer x)
     return 3x+1
End Function hailOdd

Main method
    Integer Command
    Do
        Show.Output("Enter integer (-1 to exit): ")
        Command = Get.InputStream
        If Command % 2 = 0 // This is even
           hailEven(Command)
        Else // Odd
           hailOdd(Command)
        End If
    While (Command is not -1)
End Main
I hope that helps

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #3 (permalink)  
Old 11-03-2007, 02:28 AM
writtinfool writtinfool is offline
Novice
Join Date: Jun 2007
Posts: 3
iTrader: (0)
writtinfool is on a distinguished road
a solution

"""
Here's a solution that uses a bastardized "do while" loop(without out the do)
and recursion. you need the recursion to reprocess the result.
do while loops have been considered unpythonic, although that attitude that is changing
"""
Code:
x = int(raw_input("Please enter a starting value:"))

def hail(x):
    if x % 2 == 0:
        return x / 2
    else:
        return (3*x)+1

while True:
    x = hail(x)    # recursive function call
    output = x  # don't really need the extra variable but it makes things clearer
    print output
    if output == 1:
        break

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by TeraTask : 11-03-2007 at 01:33 PM. Reason: Added code tags
Reply With Quote
The Following 2 Users Say Thank You to writtinfool For This Useful Post:
HelloWorld (11-03-2007), TeraTask (11-03-2007)
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 11:34 AM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50