473,513 Members | 2,560 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using While Loop

48 New Member
If I want to use while loop instead for loop in below program, how would I do that?

Andy

Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. heads = 0
  4. for i in range(100):
  5.  heads+=random.randrange(2)
  6. tails = (100-heads)
  7.  
  8. print "The coin landed on heads " + str(heads)+ " times and on tails " + str(tails) + " times"
  9.  
Dec 6 '06 #1
3 3306
bartonc
6,596 Recognized Expert Expert
If I want to use while loop instead for loop in below program, how would I do that?

Andy

Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. heads = 0
  4. for i in range(100):
  5.  heads+=random.randrange(2)
  6. tails = (100-heads)
  7.  
  8. print "The coin landed on heads " + str(heads)+ " times and on tails " + str(tails) + " times"
  9.  
As being discussed in a current thread, for is you best choice here. But as an example of how while work, I'll modify your loop in a couple of ways:
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. heads = 0
  4. i = 0    # zero thru 99
  5. while i < 100:
  6.     # it is usual to use the counter for something
  7.     heads+=random.randrange(2)
  8.     i += 1    # increment the counter
  9.  
  10. heads = 0
  11. i = 0    # 1 thru 100 using break
  12. while True:
  13.     i += 1    # maybe you don't want to start at zero for some reason
  14.     heads+=random.randrange(2)
  15.     if i >= 100:
  16.         break
Dec 7 '06 #2
coolindienc
48 New Member
Thanks bartonc!!!! you r the best
Dec 7 '06 #3
bartonc
6,596 Recognized Expert Expert
Thanks bartonc!!!! you r the best
Any time, really,
Barton
Dec 7 '06 #4

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

Similar topics

3
1940
by: Carlos Ribeiro | last post by:
As a side track of my latest investigations, I began to rely heavily on generators for some stuff where I would previsouly use a more conventional approach. Whenever I need to process a list, I'm tending towards the use of generators. One good example is if I want to print a report, or to work over a list with complex processing for each...
3
23998
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked tables in the database where the code resides. If we move the database with the data tables to a new directory, the links are no longer valid. I...
8
3976
by: doomx | last post by:
I'm using SQL scripts to create and alter tables in my DB I want to know if it's possible to fill the description(like in the Create table UI) using these scripts. EX: CREATE TABLE( Pk_myPrimaryKey INTEGER CONSTRAINT pk PRIMARY KEY DESCRIPTION 'This is the primary key of the table',
5
5962
by: John Dumais | last post by:
Hello, I have been trying to figure out how to write an array of doubles (in this specific case) to a binary stream without using a loop. What I have been doing is... foreach(double d in TraceData) { instanceOfBinaryWriter.Write(d); }
13
1764
by: Bev in TX | last post by:
We are using Visual Studio .NET 2003. When using that compiler, the following example code goes into an endless loop in the "while" loop when the /Og optimization option is used: #include <stdlib.h> int resize(int *incsize, int min_size) { while(*incsize <= min_size) { *incsize = (int)(*incsize * 1.25); } if ( min_size > 60 ) return...
9
3065
by: George McCullen | last post by:
I have an Outlook 2003 using Exchange Server 2003 Public Contacts Folder containing 20,000 Contacts. I am writing a VB .Net 2003 program that loops through all the contacts in a "for each oCt in oItems" loop. After about 250 contacts, the program gives an InvalidCastException Error on the for each loop. I notice that Outlook's memory keeps...
17
3031
by: John Salerno | last post by:
I'm reading Text Processing in Python right now and I came across a comment that is helping me to see for loops in a new light. I think because I'm used to the C-style for loop where you create a counter within the loop declaration, for loops have always seemed to me to be about doing something a certain number of times, and not about...
1
7729
by: Thiero | last post by:
Hi I posted s thread but did have any reply, I am a new programmer and really wants someone to help me on how to use TreeMap for this code cos I want to it to be able to handle the options from 6 to 9 in the menu of options: /*the next method to be implemented is the method presentResultsToUser() which is expected to print a menu of...
29
5056
by: garyusenet | last post by:
I'm trying to investigate the maximum size of different variable types. I'm using INT as my starting variable for exploration. I know that the maximum number that the int variable can take is: 65,535. But i'm trying to write a program to test this, assuming I didn't know this number in advance. I came up with the following but have two...
3
33332
by: Akira | last post by:
I noticed that using foreach is much slower than using for-loop, so I want to change our current code from foreach to for-loop. But I can't figure out how. Could someone help me please? Current code is here: foreach ( string propertyName in ht.Keys ) { this.setProperty( propertyName, ht );
0
7270
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7178
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7565
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7128
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7543
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5704
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3242
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
817
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.