10 Replies - 681 Views - Last Post: 27 September 2011 - 12:14 PM Rate Topic: -----

#1 sitdownson   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 16
  • Joined: 27-September 11

New to programming, need help with a question please

Posted 27 September 2011 - 11:14 AM

I have a problem for an introductory programming class that I just have no idea where to start or how to go about completing it. The question is:


a. Write a function mult() that prompts the user to enter a number greater
than 0 and then multiplies all the integers between 1 and the number (inclusive)
and prints the result of multiplying all the numbers. Hint: Use a while loop to
iterate through the numbers. Think “count with while”.

b. Modify your program so that it only multiplies odd integer

Any help would be greatly appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: New to programming, need help with a question please

#2 Motoma   User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 798
  • Joined: 08-June 10

Re: New to programming, need help with a question please

Posted 27 September 2011 - 11:26 AM

Post your code here and we'll help you figure out your issues.
Was This Post Helpful? 0
  • +
  • -

#3 sitdownson   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 16
  • Joined: 27-September 11

Re: New to programming, need help with a question please

Posted 27 September 2011 - 11:43 AM

Okay, here is what I have so far but whenever I call it I just a "1" to return

number = int(raw_input("Enter a number greater than 0: "))

def mult(number):
    ret = 1
    for i in range(1,number):
        ret = ret*i
        return ret

print mult(number)


Was This Post Helpful? 0
  • +
  • -

#4 Motoma   User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 798
  • Joined: 08-June 10

Re: New to programming, need help with a question please

Posted 27 September 2011 - 11:47 AM

The indentation of your return statement on line 7 is wrong. You want to return when you are done with the for loop; currently you return immediately after the first iteration.
number = int(raw_input("Enter a number greater than 0: "))

def mult(number):
    ret = 1
    for i in range(1,number):
        ret = ret*i
    return ret

print mult(number)


Was This Post Helpful? 1
  • +
  • -

#5 sitdownson   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 16
  • Joined: 27-September 11

Re: New to programming, need help with a question please

Posted 27 September 2011 - 11:55 AM

View PostMotoma, on 27 September 2011 - 11:47 AM, said:

The indentation of your return statement on line 7 is wrong. You want to return when you are done with the for loop; currently you return immediately after the first iteration.
number = int(raw_input("Enter a number greater than 0: "))

def mult(number):
    ret = 1
    for i in range(1,number):
        ret = ret*i
    return ret

print mult(number)



Ok thanks I didnt even notice that :P. Now I am having trouble with factorials. Not exactly sure how to implement it in which I believe is what I have to do.
Was This Post Helpful? 0
  • +
  • -

#6 stackoverflow   User is offline

  • D.I.C Addict
  • member icon

Reputation: 166
  • View blog
  • Posts: 545
  • Joined: 06-July 11

Re: New to programming, need help with a question please

Posted 27 September 2011 - 12:00 PM

Curious, but why use raw_input() if you are going to convert it to an int? May as well use input() which returns an int by default, no?
Was This Post Helpful? 0
  • +
  • -

#7 Motoma   User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 798
  • Joined: 08-June 10

Re: New to programming, need help with a question please

Posted 27 September 2011 - 12:02 PM

View Postsitdownson, on 27 September 2011 - 02:55 PM, said:

Now I am having trouble with factorials. Not exactly sure how to implement it in which I believe is what I have to do.


That code is factorial.
Was This Post Helpful? 1
  • +
  • -

#8 sitdownson   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 16
  • Joined: 27-September 11

Re: New to programming, need help with a question please

Posted 27 September 2011 - 12:05 PM

View Poststackoverflow, on 27 September 2011 - 12:00 PM, said:

Curious, but why use raw_input() if you are going to convert it to an int? May as well use input() which returns an int by default, no?


I'm in 2.7 so I think I have to. I think that is only in 3
Was This Post Helpful? 0
  • +
  • -

#9 stackoverflow   User is offline

  • D.I.C Addict
  • member icon

Reputation: 166
  • View blog
  • Posts: 545
  • Joined: 06-July 11

Re: New to programming, need help with a question please

Posted 27 September 2011 - 12:06 PM

[[email protected] ~]$ python2
Python 2.7.2 (default, Jun 29 2011, 11:17:09) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x = input("enter x:")
enter x:3
>>> print x
3


Works in 2.7.2
Was This Post Helpful? 0
  • +
  • -

#10 sitdownson   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 16
  • Joined: 27-September 11

Re: New to programming, need help with a question please

Posted 27 September 2011 - 12:09 PM

View PostMotoma, on 27 September 2011 - 12:02 PM, said:

View Postsitdownson, on 27 September 2011 - 02:55 PM, said:

Now I am having trouble with factorials. Not exactly sure how to implement it in which I believe is what I have to do.


That code is factorial.


Hmm for some reason I thought I had to have something else. Nevermind just realized it is done I believe lol

View Poststackoverflow, on 27 September 2011 - 12:06 PM, said:

[[email protected] ~]$ python2
Python 2.7.2 (default, Jun 29 2011, 11:17:09) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x = input("enter x:")
enter x:3
>>> print x
3


Works in 2.7.2


Hmm I'll try it out when I get back to my room. Thanks for the help guys
Was This Post Helpful? 0
  • +
  • -

#11 stackoverflow   User is offline

  • D.I.C Addict
  • member icon

Reputation: 166
  • View blog
  • Posts: 545
  • Joined: 06-July 11

Re: New to programming, need help with a question please

Posted 27 September 2011 - 12:14 PM

I double checked, just to clarify:

raw_input works in 2.7, returns string
input works in 2.7, returns int

raw_input doesn't work in 3.0
input works in 3.0 but returns string not an int

A cross-version form would be: int(input("enter.."))

Appears to work in both and get the effect we want. Slightly redundant in 2.7 but needed in 3.0. Unless someone knows another way to make it work in both versions~ :D

This post has been edited by stackoverflow: 27 September 2011 - 12:18 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1