473,320 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Newbie question about for...in range() structure

sp
Hi there,

I'm new to Python and I've been writing a rudimentary exercise in
Deitel's Python: How to Program, and I found that this code exhibits a
weird behavior. When I run the script, the for...range(1,11) structure
always starts with zero--and I've tried plugging other values as well!
From the Python commandline, though, this structure produces a normal
1,2,3,4,5,6,7,8,9,10 output. Can anyone tell me what I am doing wrong
here?

input = raw_input("Please enter a value you wish to factorialize:")

def fact(n):
total = 0
n = int(n)
while n 0:
total *= n
n -=1
return total

print "%d" % fact(input)

e = 1
for val in range(1,11):
if val == 0:
continue
else:
e += 1/fact(val)

print "Where n is 10, e = %d\n\n" % e
e2 = 1
input = raw_input("Enter x:")

for val in range(1,11):
if val == 0:
continue
else:
e2 += (float(input) ** val)/(fact(val))

print "Where the n is 10, e**x = %d\n\n" %e
Jun 27 '08 #1
3 949
On Apr 17, 3:30*am, "sp@k" <spockspla...@gmail.comwrote:
def fact(n):
* * * * total = 0
* * * * n = int(n)
* * * * while n 0:
* * * * * * * * total *= n
* * * * * * * * n -=1
* * * * return total
My guess is that you want to initialize total to 1, not 0.

-- Paul
Jun 27 '08 #2
sp
My guess is that you want to initialize total to 1, not 0.
D'oh! Yes, you are right, thank you.

Jun 27 '08 #3
sp@k a écrit :
(snip - already answered)
>
def fact(n):
total = 0
n = int(n)
while n 0:
total *= n
n -=1
return total
You may be interested in a very different way to get the same result:

from operator import mul

def fact(n):
return reduce(mul, xrange(1, n+1), 1)
(snip)
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
by: Chris Dutrow | last post by:
I searched around on the net for a bit, couldn't find anything though. I would like to find some code for a function where I input A Range Of Integers For example: Function( 1, 100 ); And the...
1
by: wadacom | last post by:
I'm sorry to take your time for newbie problems but I've been searching what to do about the problem with my apache server I have. I work with ubuntu dapperdrake I put the last apache server on it...
13
by: Alan Wright | last post by:
Hi Folks, I am newbie to Python, but have successfully created a simple client and server setup, I have one issue though. I am trying to test a box by sending many TCP conns (WHILE loop) but not...
7
by: idiolect | last post by:
Hi all - Sorry to plague you with another newbie question from a lurker. Hopefully, this will be simple. I have a list full of RGB pixel values read from an image. I want to test each RGB band...
1
by: Mark B | last post by:
This is my first try at using AJAX. I want the calendars to be enabled if the user checks CheckBox1. It works OK for a normal all page refresh but once I introduced the AJAX code it stopped...
18
by: sam | last post by:
(newbie)Technically what's the difference between memset() and memcpy() functions?
5
by: Albert-jan Roskam | last post by:
Hi, I wrote the program below to merge all xls files in a given directory into one multisheet xls file. It uses xlwt and xlrd. The xls files I use for input are generated by Spss. When I open and...
0
by: Albert-jan Roskam | last post by:
Hi John, Thanks! Using a higher xlrd version did the trick! Regarding your other remarks: -yep, input files with multiple sheets don't work yet. I kinda repressed that ;-) Spss outputs only...
0
by: remya1000 | last post by:
i'm using VB.Net 2005 application program. i'm trying to convert VB6 code to VB.Net 2005. QSockB is DLL file. this is the code i used for VB6. This is code i'm using to create socket, when...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.