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

Need help coding?

Okay, just to let you know, I am a certified noob, so I'm not really sure what to do. Here's what I"m supposed to do. Write a function three_heads that stimulates flipping a coin repeatedly printing what is seen (H for heads, T for tails). When 3 heads are flipped in a row, a message is printed. Here is a sample output:

T
H
T
H
H
H
Three Heads in a row!



This isn't homework, but a review question that we didn't go over in class. Just want to satisfy my curious mind. Any possible answers? Thanks.

I'm thinking that it should be like:
Expand|Select|Wrap|Line Numbers
  1. # random chooses Heads or tails
  2. from random import*
  3.  
  4. def three_heads():
  5. while(True):
  6.      possiblity=ranint(1,2)
  7.      if (possibilty==1):
  8.             print ("H")
  9.      elif (possibility==2):
  10.             print ("T")

But now I'm stuck. Any ideas? Not asking for solution, just help/hints.
Nov 25 '09 #1
5 1835
nicstel
20
A lot of solutions is possible to do what you want to do. Try this one and tell me if you understand the function that I have built.

Expand|Select|Wrap|Line Numbers
  1.  
  2. import random
  3. import string
  4.  
  5. first_time = ""
  6. second_time = ""
  7. third_time = ""
  8.  
  9. def head_or_tails():
  10.     A = random.choice(["Head", "Tails"])
  11.     return A
  12.  
  13. for B in range(100):
  14.     C = head_or_tails()
  15.     print C
  16.     if C == first_time:
  17.         second_time = C
  18.         if C == second_time:
  19.             third_time = C
  20.             if C == third_time:
  21.                 first_time = ""
  22.                 second_time = ""
  23.                 third_time = ""
  24.                 print "Three " + str(C) + " in a row!"
  25.     first_time = C
  26.  
  27.  
Nov 25 '09 #2
Well, I understand some of it, but could you explain each line. More specifically, line 3(why do you need to import string?),line 11 (what does "return A do?), and the whole for loop. Thanks!
Nov 26 '09 #3
nicstel
20
ccgrl451,

line 3: In this case is not necessary to import string. I use it often. But if you want to remove import string you need to fix line 24. Replace str(C) by C.

line 11: When you want to extract information from a function you need to do a return of the answer. A = Head or Tails.

loop: I have create three variables. first_time, second_time and third_time. When these variables are identical, it's mean that you have three Head or Tails in row.

You have a lot of possibilities. I have simplify the code.

Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. first_time = ""
  4. second_time = ""
  5. third_time = ""
  6.  
  7. for B in range(10):
  8.     C = random.choice(["Head", "Tails"])
  9.     print C
  10.     if C == first_time:
  11.         second_time = C
  12.         if C == second_time:
  13.             third_time = C
  14.             if C == third_time:
  15.                 first_time = ""
  16.                 second_time = ""
  17.                 third_time = ""
  18.                 print C
  19.                 print "Three " + C + " in a row!"
  20.     first_time = C
  21.  
Nov 26 '09 #4
bvdet
2,851 Expert Mod 2GB
Following is another possibility:
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. tosses = 'XXX'
  4. dd = {"H": "Heads", "T": "Tails"}
  5. while True:
  6.     toss = random.choice(["H", "T"])
  7.     tosses += toss
  8.     tosses = tosses[1:]
  9.     print toss
  10.     if tosses in ["HHH", 'TTT']:
  11.         print 'Three %s in a row!' % (dd[tosses[0]])
  12.         break
Nov 27 '09 #5
Glenton
391 Expert 256MB
Hey. You were really close actually. You had the random generator going.

All you needed was to keep track of the throws over time and then break out of the loop when you had success.

So add a tosses="" above your while loop.
Add a tosses+="H" above or below print("H") and similarly for "T"
Add a if "TTT" in tosses: break

And you're done!
Dec 2 '09 #6

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

Similar topics

55
by: Alex | last post by:
Hello people, The following is not a troll but a serious request. I found myself in a position where I have to present a Pro/Con list to management and architects in our company with regard to...
5
by: James | last post by:
If I want to just run basic ASP scripts and set up virtual directories on a new machine, do I need IIS or is there some other utility I can use to make this possible? XP Pro...(sans IIS) Thanks
19
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate...
4
by: Web_PDE_Eric | last post by:
I don't know where to go, or what to buy, so plz re-direct me if I'm in the wrong place. I want to do high performance integration of partial differential eqns in n dimensions (n=0,1,2,3..etc) I...
14
by: key9 | last post by:
Hi All On coding , I think I need some basic help about how to write member function . I've readed the FAQ, but I am still confuse about it when coding(reference / pointer /instance) , so I...
2
by: Tony Girgenti | last post by:
Hello. I'm trying to develop a web comsuming client using VS.NET 2003(VB), .NET Framework 1.1.4322, ASP.NET 1.1.4322, WSE2.0 on a WinXP Pro Sp2 comuter. I want to use a web form for this...
0
by: serpius | last post by:
Hello Everyone, I am a beginner in VB.Net 2003 and am taking classes in beginning VB.net. To make a long story short, I am looking for real samples of coding in Console Apps. I am the type of...
11
by: Peted | last post by:
Im using c# 2005 express edition Ive pretty much finished an winforms application and i need to significantly improve the visual appeal of the interface. Im totaly stuck on this and cant seem...
1
by: bazbella | last post by:
Hi I Need Help To Make A Scroll Box For My Web Page Also Need To Put Some Coding In To It Of A Banner For Poeple To Copy To Display Our Banner On There Page (my Space) Is There Some Sort Of...
3
by: dorandoran | last post by:
PAY PERIOD NAVIGATION ===================== I need to have 2 combo boxes (cboFrom and cboTo). these two boxes need to fill with fromdate and todate from a table "tblPayPeriods". sample pic is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
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,...

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.