Python homework help

Fruc

Junior Member
Oct 8, 2012
2
0
0
Hello.
First of all, I am not a native English speaker, so please forgive me for my bad English.

We received a homework in which we must write a program that flips a coin five times. Program then writes how many times heads has fallen.
(So, if it is... Heads, heads, tails, heads, tails... the program would print 3)

I wrote this code and I'm sure it should be working, but it only prints some random numbers (sometimes they are correct).


Code:
from flip import flip

i = 0
s = 0

while i<5:
    flip()
    i = i +1
    print (flip())
    if flip() == "H":
        s = s + 1
print (s)

flip is a code we received at school and it simulates coin-flipping:

Code:
import random

def flip():
    return random.choice("HT")
 
Last edited:

dighn

Lifer
Aug 12, 2001
22,820
4
81
Code:
    print (flip())
    if flip() == "H":

each time you call flip(), it can return a different value. so you could get a "H" when you call print(), but a "T" when you check it against "H".

you want to call flip once per iteration, storing the result. e.g.

Code:
    result = flip()
    print (result)
    if result == "H":
 

TheRyuu

Diamond Member
Dec 3, 2005
5,479
14
81
Code:
flip()
i = i +1
print (flip())
if flip() == "H":

You're actually having flip return a number 3 times here, with 3 different ((pseudo)random) results.

As dighn has already alluded to for each iteration of the loop call flip once and store that result in a variable that you can then use however you want later on.

Other random things, stuff like:
Code:
i = i + 1
s = s + 1
Can be made simpler:
Code:
i += 1
s += 1
And even simpler still:
Code:
i++
s++
The end result will be the same (and the compiler will most likely produce the same (bytecode/assembly) output in each case).
 

Cerb

Elite Member
Aug 26, 2000
17,484
33
86
And even simpler still:
Code:
i++
s++
The end result will be the same (and the compiler will most likely produce the same (bytecode/assembly) output in each case).
The above will either work in an unexpected way, or give a syntax error, depending on what else is going on in the same line. It should always give an error, but sometimes it 'works', instead.

Also, OP: what programming background were you supposed to have had up to this point? Both the flip() thing and the while loop make me think you might need to do some learning outside of the course, for more fundamental issues like context and volatility/side-effects.
 

Fruc

Junior Member
Oct 8, 2012
2
0
0
Thanks everyone for your help.

We have a basic programming background. We know how to use basic functions and mathematic signs such as:

Code:
print
input
float(input)
+  -  *  **  /  //  %
<  >  <=  >=  ==  !=

We know how to work with strings and we also learned a little about while (for how long it is executing ... )
 

Cerb

Elite Member
Aug 26, 2000
17,484
33
86
That's minimal knowledge for writing some Python, and not at all what I meant (but, pretty much what I was expecting :p). Here are a few links you should read over. The first two should be fairly easy to understand, and explain, in a very basic way, what to think about so you never make that kind of mistake with the many flips, again.

http://en.wikipedia.org/wiki/Referential_transparency_(computer_science)
http://en.wikipedia.org/wiki/Side_effect_(computer_science)

http://en.wikipedia.org/wiki/Closure_(computer_science)
This may take some time to get your head around, which is OK. However, it's important, in part because you are using a random number generator, which happens to be the classic text book case for closures. And, once you grok closures, the whole act of programming will make far more sense than it did before.

When you call a random number generator, it increments, comes up with a new number, and returns a new number (this explanation avoids real messy details and caching, of course :)). The state of the random number generator is contained within some abstraction that you can't mess with. Your interface is just the random number function(s). But, each time you call it, there are state changes behind the scenes. You need to know what functions may have such state changes, or risk such state changes, so that you only call them precisely when you need to.
 

BigDH01

Golden Member
Jul 8, 2005
1,631
88
91
List comprehensions in Python are also fun. I think the following should work.

Code:
heads = [x for x in xrange(5) if flip() == 'H']
print heads # tells you which rolls gave heads
print len(heads) # tells you how many heads were rolled
 

Fallen Kell

Diamond Member
Oct 9, 1999
6,249
561
126
And, once you grok closures, the whole act of programming will make far more sense than it did before.

Someone did not read the initial post that Fruc is not a native English speaker and then used a word like "grok" (heck, even for the native English speakers, many will not know this word). "Grok" means understand (for the most part).
 

Cerb

Elite Member
Aug 26, 2000
17,484
33
86
Someone did not read the initial post that Fruc is not a native English speaker and then used a word like "grok" (heck, even for the native English speakers, many will not know this word). "Grok" means understand (for the most part).
I read it...I just am a native English speaker, and am not the least bit multilingual.

Next, you'll probably tell me some people won't understand fnord! ;)
 

Obsoleet

Platinum Member
Oct 2, 2007
2,181
1
0
Never apologize for speaking English as a 2nd language, until all Brits speak Welsh/Gaelic and Americans speak Navajo.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Never apologize for speaking English as a 2nd language, until all Brits speak Welsh/Gaelic and Americans speak Navajo.

Why Navajo? The first native Americans encountered by the Europeans were Iroquois Nation or Leni Lenape, as far as I recall. Or possibly Caribs if you want to call the Caribbean "America."
 

TheRyuu

Diamond Member
Dec 3, 2005
5,479
14
81
The above will either work in an unexpected way, or give a syntax error, depending on what else is going on in the same line. It should always give an error, but sometimes it 'works', instead.

Oh? You can't do var++ in python? Sorry, don't have much experience with it. :p
 

Obsoleet

Platinum Member
Oct 2, 2007
2,181
1
0
Why Navajo? The first native Americans encountered by the Europeans were Iroquois Nation or Leni Lenape, as far as I recall. Or possibly Caribs if you want to call the Caribbean "America."

Navajo is still spoken in larger numbers. I'm not sure on Iroquis or the others. Maybe Apache.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Why Navajo? The first native Americans encountered by the Europeans were Iroquois Nation or Leni Lenape, as far as I recall. Or possibly Caribs if you want to call the Caribbean "America."

The Navajo & Cherokee nations are some of the existing largest still using their own language.

I am sure that the poster could have chosen any Indian tribe for illustration
 
Last edited: