Click to See Complete Forum and Search --> : just posting some code for the halibut...
inkedmn
12-18-2001, 05:40 PM
i wrote this.
blarg...
#!/usr/bin/env python
import random
import sys
heads = 0
tails = 0
x = int(raw_input("Please enter the number of coin tosses you'd like: "))
if x == 0:
print "No flips, eh? whatever..."
sys.exit()
else:
while x > 0:
flip = random.randrange(1, 3)
if flip == 1:
heads = heads + 1
else:
tails = tails + 1
x = x - 1
print "heads =", heads
print "tails =", tails
w00t!
bwkaz
12-18-2001, 06:34 PM
I wrote this:
Text-mode ncurses minesweeper (http://www.csl.mtu.edu/~bwkadzba/tmines.c)
Compile with gcc -o tmines tmines.c -lncurses. Then run ./tmines
Edit: Oh yeah, it works best in a native terminal, of type Linux. But it'll also work in an xterm, it's just slower.
[ 18 December 2001: Message edited by: bwkaz ]
EyesWideOpen
12-18-2001, 07:49 PM
Here's a :cool: Perl :cool: version:
#!/usr/bin/perl
use strict;
my ($heads, $tails, $x, $flip) = 0;
my ($retoss) = "y";
while ($retoss =~ /y|yes/i) {
$retoss = "n";
print "Please enter the number of coin tosses you'd like: ";
chomp($x = <STDIN> );
if ($x == 0) {
print "No flips, eh? Whatever...";
exit 0;
} else {
while ($x > 0) {
$flip = int(rand 2) + 1;
($flip == 1) ? ($heads = $heads + 1) : ($tails = $tails + 1);
$x = $x -1;
}
}
print "heads=$heads\n";
print "tails=$tails\n";
print "Toss again? [y] ";
lc(chomp($retoss = <STDIN> ));
$retoss = "y" if (not $retoss);
($heads, $tails, $x, $flip) = 0 if ($retoss =~ /y|yes/i);
}
I tried your code but it just hang on me! :confused: Does a default Python installation include what is needed to import random? I would think that it would give me an error if that were the problem though...
EWO: worked for me, dude! And yeah, default python install should come with everything; and even if the lib wasn't there it certainly shouldn't hang. I dunno what the problemo could be..
Did you get the prompt to enter a number? How'd you try to run it? Make sure you properly copied and pasted the code... weird.
inkedmn: very nice! Mucho pride, hermano! One thing... heh, heh, heh. There's always one thing. That call to sys.exit() is unnecessary. Even if you don't want the "heads/tails" output at the end, you shouldn't need the call to sys.exit(). There's a more elegant way to avoid printing those two lines out. ...can you do it? It's very, very simple, and doesn't involve adding anything at all to the program.
C:\Python21>python c:\flipper.py
Please enter the number of coin tosses you'd like: 23423
heads = 11820
tails = 11603
Oh yeah, I haven't got linux on my box anymore. damn civ3 was like 6 million megs. sigh.
inkedmn
12-19-2001, 04:19 PM
#!/usr/bin/env python
import random
heads = 0
tails = 0
try:
x = int(raw_input("Please enter the number of coin tosses you'd like: "))
if x == 0:
print "No flips, eh? whatever..."
else:
while x > 0:
flip = random.randrange(1, 3)
if flip == 1:
heads = heads + 1
else:
tails = tails + 1
x = x - 1
except ValueError: print "Not a Numeric Value"
if heads > 0 and tails > 0:
print "heads =", heads
print "tails =", tails
else:
print "Goodbye..."
w00t!
Gaccm
12-19-2001, 07:56 PM
#include <iostream.h>
#include <stdlib.h>
int main()
{
int x,i;
int heads=0;
int tails=0;
cout << "Please enter the number of coin tosses you'd like: ";
cin >> x;
if(x==0){
cout << "No flips, eh? whatever...";
return 0;
}
else {
while(X>0){
i=rand()%2;
if(i==0)
heads++;
else
tails++;
x--;
}
cout << "Heads= " << heads << endl << "Tails= " << tails;
}
return 0;
}
C++ w00t
[ 19 December 2001: Message edited by: Gaccm ]
ooh, handling exceptions... nice :)
Gnu/Vince
12-20-2001, 12:15 AM
#!/usr/bin/env ruby
if ARGV.size < 1 then
puts "Usage: #{$0} <Number of tosses>"
exit
end
begin
x = Integer(ARGV.at(0))
rescue ArgumentError
puts "Please input only integers"
exit
end
heads = 0
tails = 0
if x == 0 then
puts "No flips heh? Good bye"
exit
else
while x > 0 do
flip = rand(2)
if flip == 1 then heads += 1 else tails += 1 end
x -= 1
end
end
puts "Heads: #{heads}"
puts "Tails: #{tails}"
dd if=/dev/urandom of=/
wait.. OTHER random stuff.. :o
;)
Stuka
12-20-2001, 11:51 AM
kmj-
Did I read that right? Civ3??? I'm jealous as hell now! I'm a huge Civ junkie!
Originally posted by Stuka:
<STRONG>kmj-
Did I read that right? Civ3??? I'm jealous as hell now! I'm a huge Civ junkie!</STRONG>
yeah :) wiped out mandrake 7 for Civ III, and now I have loads of free space. Now I don't even have time to play it; played some marathon games during the week between fall and winter quarters (RIT has quarters, not semesters).. which was the week after it came out, luckily. :)
-sureshot-
12-20-2001, 01:57 PM
civ3 is evil. you start the game, then next thing you know its 14 hours later and you haven't eaten, urinated or studied for finals...its a damn time warp, i swear.
EyesWideOpen
12-20-2001, 02:16 PM
Originally posted by kmj:
<STRONG>Did you get the prompt to enter a number?</STRONG>
Nope.
<STRONG>How'd you try to run it?</STRONG>
I made it executable and then just ran coins.py from the command line.
I just tried it (and inkedmn's newer code) again but this time instead of executing the program I used python coins.py and it works -- with one exception. I get the following before the prompt to 'enter the number of coin tosses...': 'import site' failed; use -v for traceback.
The program still works even though I get that error though.
weird; what version of python do you have?