• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Python homework help

Fruc

Junior Member
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:
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":
 
This is a textbook example of the correct way to ask for help with homework.
 
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).
 
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.
 
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 ... )
 
That's minimal knowledge for writing some Python, and not at all what I meant (but, pretty much what I was expecting 😛). 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.
 
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
 
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).
 
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! 😉
 
Never apologize for speaking English as a 2nd language, until all Brits speak Welsh/Gaelic and Americans speak Navajo.
 
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."
 
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. 😛
 
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.
 
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:
Back
Top