473,396 Members | 1,838 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,396 software developers and data experts.

Strange issue with normal operations

Hello, I was writing a very simple code and I noticed something was failing. I detected the problematic part but I CANNOT find wheres the error.

The code is:

Expand|Select|Wrap|Line Numbers
  1. def pri(max):
  2.     for m in range(1,max):
  3.         num=(m+1)/m
  4.         if num==1: print 'ERROR'
  5.         else: print 'OK'
The problem is that it prints:
Expand|Select|Wrap|Line Numbers
  1. ERROR
  2. ERROR
  3. ERROR
  4. ERROR
  5. ERROR
  6. ERROR
  7. ERROR
  8. #...
Is there any syntax problem (I dont work with python since a long time)? Or a logical problem? Or my microprocessor just went crazy?

I hope you can help me.

Thank you very much.
Mar 7 '11 #1
5 1517
bvdet
2,851 Expert Mod 2GB
To see what is taking place, take a look at the following interaction with the interpreter:
Expand|Select|Wrap|Line Numbers
  1. >>> (1+1)/1
  2. 2
  3. >>> (2+1)/2
  4. 1
  5. >>> (3+1)/3
  6. 1
  7. >>> (4+1)/4
  8. 1
  9. >>> 
Integer division returns the floor.
Mar 7 '11 #2
But which would be the correct form?
Because neither
Expand|Select|Wrap|Line Numbers
  1. num=(m+1)/float(m)
nor
Expand|Select|Wrap|Line Numbers
  1. num=(m+1)/(m*1.0)
are correct.
Mar 7 '11 #3
bvdet
2,851 Expert Mod 2GB
Either would return a float. What would be correct?
Mar 7 '11 #4
But the problem is that they return 1, not a floating point.
Mar 7 '11 #5
bvdet
2,851 Expert Mod 2GB
I don't see the behavior you are describing.
Expand|Select|Wrap|Line Numbers
  1. >>> (1+1)/float(1)
  2. 2.0
  3. >>> (2+1)/float(2)
  4. 1.5
  5. >>> (3+1)/float(3)
  6. 1.3333333333333333
  7. >>> (1+1.)/1
  8. 2.0
  9. >>> (2+1.)/2
  10. 1.5
  11. >>> 
Mar 7 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Yaro | last post by:
Hi UDB 8.2.2 Win I am little confused about IDENTITY behavior. create table aaa( c1 integer not null generated by default as identity, c2 integer, primary key (c1)
0
by: yop | last post by:
Have a listbox Click on an item and it fills a textbox. Want to add a new item to the listbox / db I click Add, sets textbox.text = "" to clear the text type in my text and save the...
2
by: Martyn Fewtrell | last post by:
I have a repeater that displays records from a database. All fairly straight forward with the repeater binding to a dataset, and a datacommand filling the dataset from the database. The only...
1
by: Sajid | last post by:
Hello, I am trying to bind a DataTable to a Grid which will allow users to Edit data and then submit the changes back to the Database. I want to display values in the Grid satisfying a...
2
by: Joneleth | last post by:
Hi I was writing some aspx pages when i experimented a strange issue. I have a div tag like this: <div id="divCourses1" style="<%=GetDisplayStyle(eidlevel, arrIdLevel)%>"> Now, as long as i...
0
by: Wescotte | last post by:
<?php global $TABLE_GL_DATA; global $connect; $PREPARED_SQL = odbc_prepare($connect, "INSERT INTO $TABLE_GL_DATA VALUES (?,?,?,?,?,?,?)"); function Generate_GL_Data() {
13
by: Ivan Weiss | last post by:
Good morning all, I am trying to access an access database to authenticate users upon logging into my application. Something is throwing an excecption. My error handling code should display...
1
by: ahammad | last post by:
This application opens a Word document for editing. I need to keep the file path of the Word document even after it has been closed. I tried using Word event handlers but I couldn't figure it out, so...
3
by: Shisou | last post by:
Hey bytes community! this one is a really strange issue I ran into and I'm hoping you all can shine some light on it for me. This is written in C, not C++ I'm working on a program that reads...
3
by: Vish4u | last post by:
Hello Everyone, I have a encountered a strange issue with the execution of my stored procedure on clients machine. My stored procedure contains a cursor in which there is a select statement...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.